4 Copyright © 2006, Oracle. All rights reserved. Generating Reports by Grouping Related Data.

Slides:



Advertisements
Similar presentations
Using the Set Operators
Advertisements

Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
Copyright  Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
1Eyad alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
GROUP FUNCTIONS. Objectives After completing this lesson, you should be able to do the following: Identify the available group functions Describe the.
5 Copyright © 2007, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
Chapter 11 Group Functions
After completing this lesson, you should be able to do the following: Identify the available group functions Describe the use of group functions Group.
LECTURE 10.  Group functions operate on sets of rows to give one result per group.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
18 Copyright © Oracle Corporation, All rights reserved. Advanced Subqueries.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
9 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Using the Set Operators Assist. Prof. Pongpisit Wuttidittachotti, Ph.D. Faculty.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
Enhancements to the GROUP BY Clause Fresher Learning Program January, 2012.
Database Programming Sections 5– GROUP BY, HAVING clauses, Rollup & Cube Operations, Grouping Set, Set Operations 11/2/10.
17 Copyright © Oracle Corporation, All rights reserved. Enhancements to the GROUP BY Clause.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
2 Copyright © Oracle Corporation, All rights reserved. Restricting and Sorting Data.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
4 Copyright © 2006, Oracle. All rights reserved. Restricting and Sorting Data.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
Copyright س Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
2 第二讲 Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
15 Copyright © Oracle Corporation, All rights reserved. Using SET Operators.
Copyright © 2004, Oracle. All rights reserved. Using the Set Operators.
 After completing this lesson, you should be able to do the following: ◦ Use the ROLLUP operation to produce subtotal values ◦ Use the CUBE operation.
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: –Identify the available group.
ORACLE SQL Fundamental II xpp-e-f 重點 xpp-e : Generating Reports by Grouping Related Data xpp-f : Hierarchical Retrieval.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
1 Introduction to Database Systems, CS420 SQL JOIN, Group-by and Sub-query Clauses.
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
1 Copyright © 2009, Oracle. All rights reserved. B Table Descriptions.
Using Subqueries to Solve Queries
Using Subqueries to Solve Queries
Aggregating Data Using Group Functions
Using Subqueries to Solve Queries
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
Reporting Aggregated Data Using the Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Restricting and Sorting Data
Aggregating Data Using Group Functions
Presentation transcript:

4 Copyright © 2006, Oracle. All rights reserved. Generating Reports by Grouping Related Data

4-2 Copyright © 2006, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Use the ROLLUP operation to produce subtotal values Use the CUBE operation to produce cross- tabulation values Use the GROUPING function to identify the row values created by ROLLUP or CUBE Use GROUPING SETS to produce a single result set

4-3 Copyright © 2006, Oracle. All rights reserved. Review of Group Functions Group functions operate on sets of rows to give one result per group. Example: SELECT[column,] group_function(column)... FROMtable [WHEREcondition] [GROUP BYgroup_by_expression] [ORDER BYcolumn]; SELECT AVG(salary), STDDEV(salary), COUNT(commission_pct),MAX(hire_date) FROM employees WHERE job_id LIKE 'SA%';

4-4 Copyright © 2006, Oracle. All rights reserved. Review of the GROUP BY Clause Syntax: Example: SELECT department_id, job_id, SUM(salary), COUNT(employee_id) FROM employees GROUP BY department_id, job_id ; SELECT[column,] FROMtable [WHEREcondition] [GROUP BYgroup_by_expression] [ORDER BYcolumn]; group_function(column)...

4-5 Copyright © 2006, Oracle. All rights reserved. Review of the HAVING Clause Use the HAVING clause to specify which groups are to be displayed. You further restrict the groups on the basis of a limiting condition. SELECT[column,] group_function(column)... FROMtable [WHEREcondition] [GROUP BYgroup_by_expression] [HAVING having_expression] [ORDER BYcolumn];

4-6 Copyright © 2006, Oracle. All rights reserved. GROUP BY with ROLLUP and CUBE Operators Use ROLLUP or CUBE with GROUP BY to produce superaggregate rows by cross-referencing columns. ROLLUP grouping produces a result set containing the regular grouped rows and the subtotal values. CUBE grouping produces a result set containing the rows from ROLLUP and cross-tabulation rows.

4-7 Copyright © 2006, Oracle. All rights reserved. ROLLUP Operator ROLLUP is an extension to the GROUP BY clause. Use the ROLLUP operation to produce cumulative aggregates, such as subtotals. SELECT[column,] group_function(column)... FROMtable [WHEREcondition] [GROUP BY[ROLLUP] group_by_expression] [HAVING having_expression]; [ORDER BYcolumn];

4-8 Copyright © 2006, Oracle. All rights reserved. ROLLUP Operator: Example SELECT department_id, job_id, SUM(salary) FROM employees WHERE department_id < 60 GROUP BY ROLLUP(department_id, job_id); 3 1 2

4-9 Copyright © 2006, Oracle. All rights reserved. CUBE Operator CUBE is an extension to the GROUP BY clause. You can use the CUBE operator to produce cross- tabulation values with a single SELECT statement. SELECT[column,] group_function(column)... FROMtable [WHEREcondition] [GROUP BY[CUBE] group_by_expression] [HAVING having_expression] [ORDER BYcolumn];

4-10 Copyright © 2006, Oracle. All rights reserved. CUBE Operator: Example SELECT department_id, job_id, SUM(salary) FROM employees WHERE department_id < 60 GROUP BY CUBE (department_id, job_id) ;

4-11 Copyright © 2006, Oracle. All rights reserved. GROUPING Function The GROUPING function: Is used with either the CUBE or ROLLUP operator Is used to find the groups forming the subtotal in a row Is used to differentiate stored NULL values from NULL values created by ROLLUP or CUBE Returns 0 or 1 SELECT [column,] group_function(column).., GROUPING(expr) FROM table [WHERE condition] [GROUP BY [ROLLUP][CUBE] group_by_expression] [HAVING having_expression] [ORDER BY column];

4-12 Copyright © 2006, Oracle. All rights reserved. GROUPING Function: Example SELECT department_id DEPTID, job_id JOB, SUM(salary), GROUPING(department_id) GRP_DEPT, GROUPING(job_id) GRP_JOB FROM employees WHERE department_id < 50 GROUP BY ROLLUP(department_id, job_id); 1 2 3

4-13 Copyright © 2006, Oracle. All rights reserved. GROUPING SETS The GROUPING SETS syntax is used to define multiple groupings in the same query. All groupings specified in the GROUPING SETS clause are computed and the results of individual groupings are combined with a UNION ALL operation. Grouping set efficiency: –Only one pass over the base table is required. –There is no need to write complex UNION statements. –The more elements GROUPING SETS has, the greater is the performance benefit.

4-14 Copyright © 2006, Oracle. All rights reserved. Notes Only

4-15 Copyright © 2006, Oracle. All rights reserved. GROUPING SETS : Example SELECT department_id, job_id, manager_id,avg(salary) FROM employees GROUP BY GROUPING SETS ((department_id,job_id), (job_id,manager_id)); … 1 … 2

4-16 Copyright © 2006, Oracle. All rights reserved. Notes Only

4-17 Copyright © 2006, Oracle. All rights reserved. Composite Columns A composite column is a collection of columns that are treated as a unit. ROLLUP (a,, d) Use parentheses within the GROUP BY clause to group columns, so that they are treated as a unit while computing ROLLUP or CUBE operations. When used with ROLLUP or CUBE, composite columns would require skipping aggregation across certain levels. (b, c)

4-18 Copyright © 2006, Oracle. All rights reserved. Notes Only

4-19 Copyright © 2006, Oracle. All rights reserved. Composite Columns: Example SELECT department_id, job_id, manager_id, SUM(salary) FROM employees GROUP BY ROLLUP( department_id,(job_id, manager_id)); …

4-20 Copyright © 2006, Oracle. All rights reserved.

4-21 Copyright © 2006, Oracle. All rights reserved. Concatenated Groupings Concatenated groupings offer a concise way to generate useful combinations of groupings. To specify concatenated grouping sets, you separate multiple grouping sets, ROLLUP, and CUBE operations with commas so that the Oracle server combines them into a single GROUP BY clause. The result is a cross-product of groupings from each GROUPING SET. GROUP BY GROUPING SETS(a, b), GROUPING SETS(c, d)

4-22 Copyright © 2006, Oracle. All rights reserved. … … … Concatenated Groupings: Example SELECT department_id, job_id, manager_id, SUM(salary) FROM employees GROUP BY department_id, ROLLUP(job_id), CUBE(manager_id); 12345

4-23 Copyright © 2006, Oracle. All rights reserved. Summary In this lesson, you should have learned how to use the: ROLLUP operation to produce subtotal values CUBE operation to produce cross-tabulation values GROUPING function to identify the row values created by ROLLUP or CUBE GROUPING SETS syntax to define multiple groupings in the same query GROUP BY clause to combine expressions in various ways: –Composite columns –Concatenated grouping sets

4-24 Copyright © 2006, Oracle. All rights reserved. Practice 4: Overview This practice covers using: ROLLUP operators CUBE operators GROUPING functions GROUPING SETS

4-25 Copyright © 2006, Oracle. All rights reserved.

4-26 Copyright © 2006, Oracle. All rights reserved.

4-27 Copyright © 2006, Oracle. All rights reserved.

4-28 Copyright © 2006, Oracle. All rights reserved.

4-29 Copyright © 2006, Oracle. All rights reserved.

4-30 Copyright © 2006, Oracle. All rights reserved.