320 likes | 437 Views
Introduction to Oracle SQL Using Golden. Relational Database. 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.
E N D
Relational Database • 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.
Anatomy of SQL • SELECT • FROM • WHERE • GROUP BY SELECT Column1, Column2, AVG(Column3) FROM Table1, Table2 WHERE Table1.column1 = Table2.column2 GROUP BY Column1, Column2
SELECT • 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
FROM • Used to identify the location of the data in the SELECT statement • Allows table alias names to be assigned
WHERE • 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
ORDER BY • 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
GROUP BY • 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.)
Building a Script • 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
Define and document agoal scenario(TYF3) • All travel expenditures for Fiscal Year 2005, for Budget Purpose 213901 • Enter comments into script describing your expected results
Example ofScript Documentation /* Date Created: 10/06/2005 Created by: Loren Cook Description: Selects travel expenditures for Budget Purpose 272008 for the month of July 2005 */
Break-down goal into parts • Expenditures • Travel • Time Frame • Budget Purpose
Design the output (SELECT) • 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)
Example of a SELECT statement 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"
Determine the locationof data (FROM) • Which tables or views hold the data? • What do you want to call your tables?
Example of FROM Statement FROM gl.gl_je_batches b, gl.gl_je_headers h, gl.gl_je_lines l, gl.gl_code_combinations c
Filter the output (WHERE) • 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
Example of a WHERE Statement 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'
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.
WHERE Clause Used to Join Tables 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:
Refine your results (GROUP BY) (ORDER BY) • 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?
Example of ORDER BY Statement ORDER BY c.segment3, "Dept Activity 1", c.segment8
Clean-Up • 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)
Completed Example Script /* 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
Completed Example Script (cont.) 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
Things You’ll Forget (TYF) • All tables or views must be joined together in the WHERE clause to create a single row of data. • When grouping you must include all items in the select statement unless it is an aggregate function. • 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…*/) • 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. • You can use aggregate functions (SUM, AVG, COUNT, etc.) to better summarize data for your specific needs. • 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’ • 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 (SQL-L@siu.edu).