Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups...

32
Introduction to Oracle SQL Using Golden 1

Transcript of Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups...

Page 1: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Introduction to Oracle SQLUsing Golden

1

Page 2: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Relational Database

2

Is organized in a way that groups similar information. A collection of these groups called tables. Tables consist of columns and rows similar to a

spreadsheet. The columns describe the information stored in the rows. It is the relationship between the columns that tie the

tables together. Tying or joining the tables together allows us to pull

related information from multiple tables.

Page 3: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Relational Database Table Example

Budget Purpose Table

Budget Purpose Budget Purpose Name Fiscal Officer ID

202001 Biology PSMITH

202002 Engineering TJONES

202003 Bursar JROGERS

Fiscal Officer Table

Fiscal Officer ID Fiscal Officer Name Department

JROGERS James Rogers Bursar

PSMITH Peter Smith Biology

KSTEVENS Karen Stevens Education

Transaction Table

Transaction ID Budget Purpose Debit Credit

0001 202001 0.00 1300.00

0002 202001 1300.00 0.00

0003 202003 0.00 2650.00

3

Page 4: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Relational Database Tables Joined

Budget Purpose Table

Budget Purpose Budget Purpose Name Fiscal Officer ID

202001 Biology PSMITH

202002 Engineering TJONES

202003 Bursar JROGERS

Fiscal Officer Table

Fiscal Officer ID Fiscal Officer Name Department

JROGERS James Rogers Bursar

PSMITH Peter Smith Biology

KSTEVENS Karen Stevens Education

Transaction Table

Transaction ID Budget Purpose Debit Credit

0001 202001 0.00 1300.00

0002 202001 1300.00 0.00

0003 202003 0.00 2650.00

4

Page 5: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Anatomy of SQL

5

SELECT FROM WHERE GROUP BY

SELECT Column1, Column2, AVG(Column3)

FROM Table1, Table2

WHERE Table1.column1 = Table2.column2

GROUP BYColumn1, Column2

Page 6: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

SELECT

6

Used to define what data will be retrieved Result Set – rows and columns returned Determines the vertical order columns will be displayed Allows column alias names to be assigned Allows aggregate functions to be used in conjunction with

GROUP BY Allows functions to be used to format certain types of data

Page 7: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

FROM

7

Used to identify the location of the data in the SELECT statement Allows table alias names to be assigned

Page 8: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

WHERE

8

Conditionally joins two or more tables(TYF1)

Filter out unwanted data from a query’s result set Isolate one or more rows of a table for modification

Page 9: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

ORDER BY

9

Allows the user to sort the output by any column or combination of columns

Similar to the SORT function in Excel Can sort in either ascending or descending order by adding ASC

or DESC

Page 10: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

GROUP BY

10

Refine your data by summarizing the results over a number of rows returning a single value(TYF2)

Similar to the SUBTOTALS function in Excel Allows the use of aggregate functions (SUM, COUNT, AVG, MAX,

MIN, etc.)

Page 11: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Building a Script

11

Page 12: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Building a Script

12

Define and document a goal scenario Break-down goal into manageable parts Design the output (SELECT) Determine the location of data (FROM) Filter the output (WHERE) Refine the results (ORDER BY)(GROUP BY) Clean-Up

Page 13: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Define and document agoal scenario(TYF3)

13

All travel expenditures for Fiscal Year 2005, for Budget Purpose 213901

Enter comments into script describing your expected results

Page 14: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Example ofScript Documentation

14

/*Date Created: 10/06/2005Created by: Loren CookDescription: Selects travel expenditures for Budget

Purpose 272008 for the month ofJuly 2005

*/

Page 15: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Break-down goal into parts

15

Expenditures Travel Time Frame Budget Purpose

Page 16: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Design the output (SELECT)

16

What columns do you want to see in your report? What order do you want your output to be displayed? What do you want your column headings to be? Are there any calculations that need to be done on your data? Do you need to convert DATES or NULL values to a different

format?(TYF4)

Page 17: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Example of a SELECT statement

17

SELECT c.segment3 as "Budget Purpose", c.segment4 as "Dept Activity 1", c.segment5 as "Dept Activity 2", c.segment7 as "Natural Account", c.segment8 as "Object", TO_CHAR(l.effective_date,'DD-Mon-YY') as "Ledger Date", NVL(l.accounted_dr,0) as "Debit Amount", NVL(l.accounted_cr,0) as "Credit Amount", l.description as "Description", l.reference_1 as "Reference 1", h.name as "Headers Name", b.name as "Batches Reference"

Page 18: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Determine the locationof data (FROM)

18

Which tables or views hold the data? What do you want to call your tables?

Page 19: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Example of FROM Statement

19

FROMgl.gl_je_batches b,gl.gl_je_headers h,gl.gl_je_lines l,gl.gl_code_combinations c

Page 20: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Filter the output (WHERE)

20

How do you want your result set filtered? What columns should be used to join the tables together? What order should you filter the data to give you the shortest

run time? Scripts run from bottom up in the WHERE clause. List the items from bottom up that will filter out the most data. The more data you eliminate with each condition, the less you will

have to process

Page 21: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Example of a WHERE Statement

21

WHERE

-- ****TABLE LINKING SECTION DO NOT CHANGE ****

h.je_header_id = l.je_header_id

and c.code_combination_id = l.code_combination_id

and b.je_batch_id = h.je_batch_id

-- EFFECTIVE DATE

--Enter the effective date range below.

and l.effective_date between '07/01/2004 00:00:00' and '06/30/2005 23:59:59'

-- BUDGET PURPOSE

--Enter the Budget Purpose below.

and c.segment3 = '213901'

-- OBJECT

--Enter the range of Objects below.

and c.segment8 between '4301' and '4398'

-- NATURAL ACCOUNT

--Enter the Natural Account range below.

and c.segment7 between '50000' and '60000'

-- ENCUMBRANCE TYPE

--Used to restrict Encumbrance type (A=Actuals, B=Budget, E=Encumbrance)

and h.actual_flag = 'A'

-- SET OF BOOKS

--This limits your selection to the SIU Set Of Books.

and h.set_of_books_id = '1'

Page 22: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Generic Exampleof a Table Join

The following 4 slides represent a generic example of how to join 3 tables together to create a single record set of data.

22

Page 23: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Example Tables

Budget Purpose Table

Budget Purpose Budget Purpose Name Fiscal Officer ID

202001 Biology PSMITH

202002 Engineering TJONES

202003 Bursar JROGERS

Fiscal Officer Table

Fiscal Officer ID Fiscal Officer Name Department

JROGERS James Rogers Bursar

PSMITH Peter Smith Biology

KSTEVENS Karen Stevens Education

Transaction Table

Transaction ID Budget Purpose Debit Credit

0001 202001 0.00 1300.00

0002 202001 1300.00 0.00

0003 202003 0.00 2650.00

23

Page 24: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Identify Common Key Fields to Join

Budget Purpose Table

Budget Purpose Budget Purpose Name Fiscal Officer ID

202001 Biology PSMITH

202002 Engineering TJONES

202003 Bursar JROGERS

Fiscal Officer Table

Fiscal Officer ID Fiscal Officer Name Department

JROGERS James Rogers Bursar

PSMITH Peter Smith Biology

KSTEVENS Karen Stevens Education

Transaction Table

Transaction ID Budget Purpose Debit Credit

0001 202001 0.00 1300.00

0002 202001 1300.00 0.00

0003 202003 0.00 2650.00

24

Page 25: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

WHERE Clause Used to Join Tables

25

WHEREFiscal_Officer_Table.Fiscal_Officer_ID = Budget_Purpose_Table.Fiscal_Officer_ID

ANDBudget_Purpose_Table.Budget_Purpose = Transaction_Table.Budget_Purpose

Creates a single line of information (result set) to select data from:

Page 26: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Result Set After Joining Tables

Fiscal_Officer_Table Budget_Purpose_Table Transaction_Table

Fiscal_Officer_ID

Fiscal_Officer_Name

Department Budget_Purpose

Budget_Purpose_Name

Fiscal_Officer_ID

Transaction_ID

Budget_Purpose

Debit Credit

JROGERS James Rogers Bursar 202003 Bursar JROGERS 0003 202003 0.00 2650.00

PSMITH Peter Smith Biology 202001 Biology PSMITH 0001 202001 0.00 1300.00

PSMITH Peter Smith Biology 202001 Biology PSMITH 0002 202001 1300.00 0.00

26

Page 27: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Refine your results (GROUP BY) (ORDER BY)

27

Do you want your result set grouped? Do you want to use aggregate functions to summarize your

result set? (TYF5)

What order do you want your result set to be displayed?

Page 28: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Example of ORDER BY Statement

28

ORDER BYc.segment3,

"Dept Activity 1",c.segment8

Page 29: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Clean-Up

29

Make sure you have restricted your query to only the SIU Set of Books. (TYF6)

Evaluate your results by comparing to test data. If you are not getting the expected results expand your

SELECT statement to include other columns to give you a clue to the error.

Refine your WHERE clause accordingly. E-mail the SQL Users Group Listserv ([email protected]), or

visit the Web Page (www.siu.edu/~sql)(TYF7)

Page 30: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Completed Example Script

30

/* Date Created: 10/06/2005Created by: Loren CookDescription: Selects travel expenditures for

Budget Purpose 272008 for the month of July 2005 */

SELECT segment3 as "Budget Purpose", segment4 as "Dept Activity 1", segment5 as "Dept Activity 2", segment7 as "Natural Account", segment8 as "Object", TO_CHAR(l.EFFECTIVE_DATE,'DD-Mon-YY') as "Ledger

Date", NVL(accounted_dr,0) as "Debit Amount", NVL(accounted_cr,0) as "Credit Amount", l.description as "Description", l.reference_1 as "Reference 1", h.name as "Headers Name", b.name as "Batches Reference"

FROMgl.gl_je_batches b,gl.gl_je_headers h,gl.gl_je_lines l,gl.gl_code_combinations c

Page 31: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Completed Example Script (cont.)

31

WHERE-- ****TABLE LINKING SECTION DO NOT CHANGE

****h.je_header_id = l.je_header_idand c.code_combination_id = l.code_combination_idand b.je_batch_id = h.je_batch_id

-- EFFECTIVE DATE--Enter the effective date range below.and l.EFFECTIVE_DATE between '07/01/2004 00:00:00' and '06/30/2005 23:59:59'

-- BUDGET PURPOSE--Enter the Budget Purpose below.and c.segment3 = '213901'

-- OBJECT--Enter the range of Objects below.and c.segment8 between '4301' and '4398‘

-- NATURAL ACCOUNT--Enter the Natural Account range below.and c.segment7 between '50000' and '60000'

-- ENCUMBRANCE TYPE--Used to restrict Encumbrance type (A=Actuals, B=Budget, E=Encumbrance)and h.actual_flag = 'A'

-- SET OF BOOKS--This limits your selection to the SIU Set Of Books.and h.set_of_books_id = '1'

ORDER BY"Dept Activity 1",

"Dept Activity 2",c.segment8

Page 32: Introduction to Oracle SQL Using Golden 1. Relational Database 2 Is organized in a way that groups similar information. A collection of these groups called.

Things You’ll Forget (TYF)

32

1. All tables or views must be joined together in the WHERE clause to create a single row of data.2. When grouping you must include all items in the select statement unless it is an aggregate function.3. Comments should be used in your query for documentation purposes. They can also be used to

prevent a clause from running for special purposes, such as testing. There are two ways to enter comments in your script. Enter two dashes in front of a single line of comments (i.e. --Comments…) or enclose multiple lines of comments with a slash asterisk before and an asterisk slash after (i.e. /*Comments…*/)

4. When doing calculations on data consider NULL values which may cause unexpected results. Use the NVL function to convert NULL values to a value that can be included in a calculation.

5. You can use aggregate functions (SUM, AVG, COUNT, etc.) to better summarize data for your specific needs.

6. The AIS database holds data for SIU and the SIU School of Medicine’s Physicians and Surgeons group. The “Set Of Books ID” must always be equal to ‘1’ in order to restrict your results only to SIU data. This “Set Of Books ID” shows up in several tables in AIS (gl.gl_je_headers, gl.gl_je_batches, and gl.gl_je_lines), but only needs to be restricted in one statement. You can do this in the WHERE clause by adding any of the following lines depending on the tables in use:

gl.gl_je_lines.set_of_books_id = ‘1’ gl.gl_je_batches.set_of_books_id = ‘1’ gl.gl_je_headers.set_of_books_id = ‘1’

7. The SQL Users Group is a great place to go for help. Others on campus may have run into the same problem and can help get you back on track. Don’t be afraid to e-mail a question to the group([email protected]).