SlideShare a Scribd company logo
Enhancements to the GROUP BY Clause  http://ecomputernotes.com
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  http://ecomputernotes.com
Review of Group Functions  Group functions operate on sets of rows to give one  result per group.  SELECT  [ column ,]  group_function(column). . .  FRO M  table  [WHERE  condition ]  [GROUP BY  group_by_expression ]  [ORDER BY  column ];  Example:  SELECT AVG(salary), STDDEV(salary),  COUNT(commission_pct),MAX(hire_date)  FROM  employees  WHERE  job_id LIKE 'SA%';  http://ecomputernotes.com
Review of the   GROUP BY   Clause  Syntax:  SELECT  [ column ,]  group_function(column). . .  FRO M  table  [WHERE  condition ]  [GROUP BY  group_by_expression ]  [ORDER BY  column ];  Example:  SELECT  department_id, job_id, SUM(salary),  COUNT(employee_id)  FROM  employees  GROUP BY department_id, job_id ;  http://ecomputernotes.com
Review of the   HAVING   Clause  SELECT  [ column ,]  group_function(column)...  FRO M  table  [WHERE  condition ]  [GROUP BY  group_by_expression ]  [HAVING  having_expression ]  [ORDER BY  column ];  "  Use the   HAVING   clause to specify which groups  are to be displayed.  "Y ou further restrict the groups on the basis of a  limiting condition.  http://ecomputernotes.com
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 results set containing the regular grouped rows and the  subtotal values.  "  CUBE   grouping produces a results set containing the rows from   ROLLUP   and cross-tabulation rows.  http://ecomputernotes.com
ROLLUP   Operator  SELECT  [ column ,]  group_function(column). . .  FRO M  table  [WHERE  condition ]  [GROUP BY  [ROLLUP]   group_by_expression ]  [HAVING  having_expression ];  [ORDER BY  column ];  "  ROLLUP   is an extension to the   GROUP BY   clause.  "  Use the   ROLLUP   operation to produce cumulative aggregates, such as subtotals.  http://ecomputernotes.com
ROLLUP   Operator Example  SELECT  department_id, job_id, SUM(salary) FROM  employees  WHERE  department_id < 60  GROUP BY ROLLUP(department_id, job_id);  http://ecomputernotes.com
CUBE   Operator  SELECT  [ column ,]  group_function(column)...  FRO M  table  [WHERE  condition ]  [GROUP BY  [CUBE]   group_by_expression ]  [HAVING  having_expression ]  [ORDER BY  column ];  &quot;  CUBE   is an extension to the   GROUP BY   clause.  &quot;  You can use the   CUBE   operator to produce cross- tabulation values with a single   SELECT   statement.  http://ecomputernotes.com
CUBE   Operator: Example  SELECT  department_id, job_id, SUM(salary) FROM  employees  WHERE  department_id < 60  GROUP BY CUBE (department_id, job_id) ;  2  4  http://ecomputernotes.com
GROUPING   Function  SELECT  [ column ,]  group_function(column) . ,  GROUPING(expr)  FRO M  table  [WHERE  condition ]  [GROUP BY [ROLLUP][CUBE]   group_by_expression ] [HAVING  having_expression ]  [ORDER BY   column ];  &quot;  The   GROUPING   function can be used with either the  CUBE   or   ROLLUP   operator.  &quot;U sing the  G ROUPING  f unction, you can find the  groups forming the subtotal in a row.  &quot;U sing the  G ROUPING  f unction, you can differentiate  stored   NULL   values from   NULL   values created by  ROLLUP   or   CUBE .  &quot;  The   GROUPING   function returns 0 or 1.  http://ecomputernotes.com
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);  2  3  http://ecomputernotes.com
GROUPING SETS  &quot;G ROUPING SETS  a re a further extension of the  GROUP BY   clause.  &quot;Y ou can use  G ROUPING SETS  t o define multiple  groupings in the same query.  &quot;T he Oracle Server computes all groupings specified  in the   GROUPING SETS   clause and combines the results of individual groupings with a   UNION ALL operation.  &quot;G rouping set efficiency:  Only one pass over the base table is required. There is no need to write complex UNION statements. The more elements the GROUPING SETS have, the greater the performance benefit.  http://ecomputernotes.com
http://ecomputernotes.com
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  http://ecomputernotes.com
http://ecomputernotes.com
Composite Columns  &quot;A  composite column is a collection of columns  that are treated as a unit.  ROLLUP (a,  , d)  (b,c)  &quot;  To specify composite columns, use the   GROUP BY  clause to group columns within parentheses so  that the Oracle server treats them as a unit while computing   ROLLUP   or   CUBE   operations.  &quot;W hen used with  R OLLUP  o r  C UBE,  composite  columns would mean skipping aggregation across  certain levels.  http://ecomputernotes.com
http://ecomputernotes.com
Composite Columns: Example  SELECT  department_id, job_id, manager_id,  SUM(salary)  FROM  employees  GROUP BY ROLLUP( department_id,(job_id, manager_id));  «  http://ecomputernotes.com
i
Concatenated Groupings  &quot;C oncatenated groupings offer a concise way to  generate useful combinations of groupings.  &quot;T o 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.  &quot;T he result is a cross-product of groupings from  each grouping set.  GROUP BY GROUPING SETS(a, b), GROUPING SETS(c, d)
Concatenated Groupigs Example  SELECT  department_id, job_id, manager_id, SUM(salary)  FROM  employees  GROUP BY department_id,  ROLLUP(job_id),  CUBE(manager_id);  «  « «

More Related Content

What's hot (20)

PPT
ODP
My sql Syntax
PPT
Powerpointpresentation.c
PPTX
Oracle: Functions
PPT
KEY
Decent exposure: Controladores sin @ivars
DOC
235689260 oracle-forms-10g-tutorial
PDF
PrimeNG - Components para la Vida Real
PPTX
Les04 Displaying Data From Multiple Table
PPT
Arrays in php
ODP
Prabu's sql quries
PDF
Columnrename
PDF
Fat Arrow (ES6)
PPT
Single row functions
PPT
Les08-Oracle
PPT
My sql Syntax
Powerpointpresentation.c
Oracle: Functions
Decent exposure: Controladores sin @ivars
235689260 oracle-forms-10g-tutorial
PrimeNG - Components para la Vida Real
Les04 Displaying Data From Multiple Table
Arrays in php
Prabu's sql quries
Columnrename
Fat Arrow (ES6)
Single row functions
Les08-Oracle
Ad

Similar to e computer notes - Enhancements to the group by clause (20)

PPT
PDF
Oracle Database Advanced Querying
PDF
Oracle Database Advanced Querying (2016)
PDF
The art of querying – newest and advanced SQL techniques
PDF
OOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
PDF
Oracle Advanced SQL and Analytic Functions
PPTX
Exploring Advanced SQL Techniques Using Analytic Functions
PPTX
Exploring Advanced SQL Techniques Using Analytic Functions
PPT
Oracle tips and tricks
PPT
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
PPT
Sql query [select, sub] 4
PDF
Introduction to oracle functions
PDF
Olapsql
PPSX
Analytic & Windowing functions in oracle
PPT
e computer notes - Aggregating data using group functions
PPTX
Data Base Management Slides SQL with example
PPTX
Sql server ___________session_10(group by clause)
PDF
SQL-AGG-FUN.pdfiiiijuyyttfffgyyuyyyyyhhh
PPTX
1. dml select statement reterive data
PDF
Cube rollup slides
Oracle Database Advanced Querying
Oracle Database Advanced Querying (2016)
The art of querying – newest and advanced SQL techniques
OOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
Oracle Advanced SQL and Analytic Functions
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic Functions
Oracle tips and tricks
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
Sql query [select, sub] 4
Introduction to oracle functions
Olapsql
Analytic & Windowing functions in oracle
e computer notes - Aggregating data using group functions
Data Base Management Slides SQL with example
Sql server ___________session_10(group by clause)
SQL-AGG-FUN.pdfiiiijuyyttfffgyyuyyyyyhhh
1. dml select statement reterive data
Cube rollup slides
Ad

More from ecomputernotes (20)

PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 39
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 20
PPT
computer notes - Data Structures - 15
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 31
PPT
computer notes - Data Structures - 4
PPT
computer notes - Data Structures - 13
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 16
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 35
PPT
computer notes - Data Structures - 36
computer notes - Data Structures - 30
computer notes - Data Structures - 39
computer notes - Data Structures - 11
computer notes - Data Structures - 20
computer notes - Data Structures - 15
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 28
computer notes - Data Structures - 19
computer notes - Data Structures - 31
computer notes - Data Structures - 4
computer notes - Data Structures - 13
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 16
computer notes - Data Structures - 22
computer notes - Data Structures - 35
computer notes - Data Structures - 36

Recently uploaded (20)

PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Classroom Observation Tools for Teachers
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
RMMM.pdf make it easy to upload and study
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Computing-Curriculum for Schools in Ghana
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Cell Structure & Organelles in detailed.
Pharmacology of Heart Failure /Pharmacotherapy of CHF
O5-L3 Freight Transport Ops (International) V1.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Cell Types and Its function , kingdom of life
TR - Agricultural Crops Production NC III.pdf
Basic Mud Logging Guide for educational purpose
O7-L3 Supply Chain Operations - ICLT Program
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Anesthesia in Laparoscopic Surgery in India
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Classroom Observation Tools for Teachers
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
RMMM.pdf make it easy to upload and study
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Renaissance Architecture: A Journey from Faith to Humanism
PPH.pptx obstetrics and gynecology in nursing
Computing-Curriculum for Schools in Ghana

e computer notes - Enhancements to the group by clause

  • 1. Enhancements to the GROUP BY Clause http://ecomputernotes.com
  • 2. Objectives After completing this lesson, you should be able to do the following: &quot; Use the ROLLUP operation to produce subtotal values &quot; Use the CUBE operation to produce cross- tabulation values &quot; Use the GROUPING function to identify the row values created by ROLLUP or CUBE &quot; Use GROUPING SETS to produce a single result set http://ecomputernotes.com
  • 3. Review of Group Functions Group functions operate on sets of rows to give one result per group. SELECT [ column ,] group_function(column). . . FRO M table [WHERE condition ] [GROUP BY group_by_expression ] [ORDER BY column ]; Example: SELECT AVG(salary), STDDEV(salary), COUNT(commission_pct),MAX(hire_date) FROM employees WHERE job_id LIKE 'SA%'; http://ecomputernotes.com
  • 4. Review of the GROUP BY Clause Syntax: SELECT [ column ,] group_function(column). . . FRO M table [WHERE condition ] [GROUP BY group_by_expression ] [ORDER BY column ]; Example: SELECT department_id, job_id, SUM(salary), COUNT(employee_id) FROM employees GROUP BY department_id, job_id ; http://ecomputernotes.com
  • 5. Review of the HAVING Clause SELECT [ column ,] group_function(column)... FRO M table [WHERE condition ] [GROUP BY group_by_expression ] [HAVING having_expression ] [ORDER BY column ]; &quot; Use the HAVING clause to specify which groups are to be displayed. &quot;Y ou further restrict the groups on the basis of a limiting condition. http://ecomputernotes.com
  • 6. GROUP BY with ROLLUP and CUBE Operators &quot; Use ROLLUP or CUBE with GROUP BY to produce superaggregate rows by cross-referencing columns. &quot; ROLLUP grouping produces a results set containing the regular grouped rows and the subtotal values. &quot; CUBE grouping produces a results set containing the rows from ROLLUP and cross-tabulation rows. http://ecomputernotes.com
  • 7. ROLLUP Operator SELECT [ column ,] group_function(column). . . FRO M table [WHERE condition ] [GROUP BY [ROLLUP] group_by_expression ] [HAVING having_expression ]; [ORDER BY column ]; &quot; ROLLUP is an extension to the GROUP BY clause. &quot; Use the ROLLUP operation to produce cumulative aggregates, such as subtotals. http://ecomputernotes.com
  • 8. ROLLUP Operator Example SELECT department_id, job_id, SUM(salary) FROM employees WHERE department_id < 60 GROUP BY ROLLUP(department_id, job_id); http://ecomputernotes.com
  • 9. CUBE Operator SELECT [ column ,] group_function(column)... FRO M table [WHERE condition ] [GROUP BY [CUBE] group_by_expression ] [HAVING having_expression ] [ORDER BY column ]; &quot; CUBE is an extension to the GROUP BY clause. &quot; You can use the CUBE operator to produce cross- tabulation values with a single SELECT statement. http://ecomputernotes.com
  • 10. CUBE Operator: Example SELECT department_id, job_id, SUM(salary) FROM employees WHERE department_id < 60 GROUP BY CUBE (department_id, job_id) ; 2 4 http://ecomputernotes.com
  • 11. GROUPING Function SELECT [ column ,] group_function(column) . , GROUPING(expr) FRO M table [WHERE condition ] [GROUP BY [ROLLUP][CUBE] group_by_expression ] [HAVING having_expression ] [ORDER BY column ]; &quot; The GROUPING function can be used with either the CUBE or ROLLUP operator. &quot;U sing the G ROUPING f unction, you can find the groups forming the subtotal in a row. &quot;U sing the G ROUPING f unction, you can differentiate stored NULL values from NULL values created by ROLLUP or CUBE . &quot; The GROUPING function returns 0 or 1. http://ecomputernotes.com
  • 12. 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); 2 3 http://ecomputernotes.com
  • 13. GROUPING SETS &quot;G ROUPING SETS a re a further extension of the GROUP BY clause. &quot;Y ou can use G ROUPING SETS t o define multiple groupings in the same query. &quot;T he Oracle Server computes all groupings specified in the GROUPING SETS clause and combines the results of individual groupings with a UNION ALL operation. &quot;G rouping set efficiency: Only one pass over the base table is required. There is no need to write complex UNION statements. The more elements the GROUPING SETS have, the greater the performance benefit. http://ecomputernotes.com
  • 15. 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 http://ecomputernotes.com
  • 17. Composite Columns &quot;A composite column is a collection of columns that are treated as a unit. ROLLUP (a, , d) (b,c) &quot; To specify composite columns, use the GROUP BY clause to group columns within parentheses so that the Oracle server treats them as a unit while computing ROLLUP or CUBE operations. &quot;W hen used with R OLLUP o r C UBE, composite columns would mean skipping aggregation across certain levels. http://ecomputernotes.com
  • 19. Composite Columns: Example SELECT department_id, job_id, manager_id, SUM(salary) FROM employees GROUP BY ROLLUP( department_id,(job_id, manager_id)); « http://ecomputernotes.com
  • 20. i
  • 21. Concatenated Groupings &quot;C oncatenated groupings offer a concise way to generate useful combinations of groupings. &quot;T o 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. &quot;T he result is a cross-product of groupings from each grouping set. GROUP BY GROUPING SETS(a, b), GROUPING SETS(c, d)
  • 22. Concatenated Groupigs Example SELECT department_id, job_id, manager_id, SUM(salary) FROM employees GROUP BY department_id, ROLLUP(job_id), CUBE(manager_id); « « «