Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Introduction to Oracle SQL Using Golden 1

2 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.

3 Relational Database Table Example Budget Purpose Table Budget PurposeBudget Purpose NameFiscal Officer ID 202001BiologyPSMITH 202002EngineeringTJONES 202003BursarJROGERS Fiscal Officer Table Fiscal Officer IDFiscal Officer NameDepartment JROGERSJames RogersBursar PSMITHPeter SmithBiology KSTEVENSKaren StevensEducation Transaction Table Transaction IDBudget PurposeDebitCredit 00012020010.001300.00 00022020011300.000.00 0003202003 0.002650.00 3

4 Relational Database Tables Joined Budget Purpose Table Budget PurposeBudget Purpose NameFiscal Officer ID 202001BiologyPSMITH 202002EngineeringTJONES 202003BursarJROGERS Fiscal Officer Table Fiscal Officer IDFiscal Officer NameDepartment JROGERSJames RogersBursar PSMITHPeter SmithBiology KSTEVENSKaren StevensEducation Transaction Table Transaction IDBudget PurposeDebitCredit 00012020010.001300.00 00022020011300.000.00 0003202003 0.002650.00 4

5 Anatomy of SQL 5 SELECT FROM WHERE GROUP BY SELECT Column1, Column2, AVG(Column3) FROM Table1, Table2 WHERE Table1.column1 = Table2.column2 GROUP BY Column1, Column2

6 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

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

8 WHERE 8 Conditionally joins two or more tables (TYF1) Filter out unwanted data from a querys result set Isolate one or more rows of a table for modification

9 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

10 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.)

11 Building a Script 11

12 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

13 Define and document a goal scenario (TYF3) 13 All travel expenditures for Fiscal Year 2005, for Budget Purpose 213901 Enter comments into script describing your expected results

14 Example of Script Documentation 14 /* Date Created:10/06/2005 Created by:Loren Cook Description:Selects travel expenditures for Budget Purpose 272008 for the month of July 2005 */

15 Break-down goal into parts 15 Expenditures Travel Time Frame Budget Purpose

16 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)

17 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"

18 Determine the location of data (FROM) 18 Which tables or views hold the data? What do you want to call your tables?

19 Example of FROM Statement 19 FROM gl.gl_je_batches b, gl.gl_je_headers h, gl.gl_je_lines l, gl.gl_code_combinations c

20 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

21 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'

22 Generic Example of 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

23 Example Tables Budget Purpose Table Budget PurposeBudget Purpose NameFiscal Officer ID 202001BiologyPSMITH 202002EngineeringTJONES 202003BursarJROGERS Fiscal Officer Table Fiscal Officer IDFiscal Officer NameDepartment JROGERSJames RogersBursar PSMITHPeter SmithBiology KSTEVENSKaren StevensEducation Transaction Table Transaction IDBudget PurposeDebitCredit 00012020010.001300.00 00022020011300.000.00 0003202003 0.002650.00 23

24 Identify Common Key Fields to Join Budget Purpose Table Budget PurposeBudget Purpose NameFiscal Officer ID 202001BiologyPSMITH 202002EngineeringTJONES 202003BursarJROGERS Fiscal Officer Table Fiscal Officer IDFiscal Officer NameDepartment JROGERSJames RogersBursar PSMITHPeter SmithBiology KSTEVENSKaren StevensEducation Transaction Table Transaction IDBudget PurposeDebitCredit 00012020010.001300.00 00022020011300.000.00 0003202003 0.002650.00 24

25 WHERE Clause Used to Join Tables 25 WHERE Fiscal_Officer_Table.Fiscal_Officer_ID = Budget_Purpose_Table.Fiscal_Officer_ID AND Budget_Purpose_Table.Budget_Purpose = Transaction_Table.Budget_Purpose Creates a single line of information (result set) to select data from:

26 Result Set After Joining Tables Fiscal_Officer_TableBudget_Purpose_TableTransaction_Table Fiscal_Officer_I D Fiscal_Officer_ Name DepartmentBudget_P urpose Budget_Purpose_ Name Fiscal_Officer_I D Transaction_I D Budget_P urpose DebitCredit JROGERSJames RogersBursar202003BursarJROGERS00032020030.002650.00 PSMITHPeter SmithBiology202001BiologyPSMITH00012020010.001300.00 PSMITHPeter SmithBiology202001BiologyPSMITH00022020011300.000.00 26

27 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?

28 Example of ORDER BY Statement 28 ORDER BY c.segment3, "Dept Activity 1", c.segment8

29 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 (SQL-L@siu.edu), or visit the Web Page (www.siu.edu/~sql) (TYF7)SQL-L@siu.eduwww.siu.edu/~sql

30 Completed Example Script 30 /*Date Created:10/06/2005 Created by:Loren Cook Description: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" FROM gl.gl_je_batches b, gl.gl_je_headers h, gl.gl_je_lines l, gl.gl_code_combinations c

31 Completed Example Script (cont.) 31 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' ORDER BY "Dept Activity 1", "Dept Activity 2", c.segment8

32 Things Youll 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 Medicines 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. Dont be afraid to e-mail a question to the group (SQL-L@siu.edu).SQL-L@siu.edu


Download ppt "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."

Similar presentations


Ads by Google