0% found this document useful (0 votes)
118 views

Grouping and Aggregating Data Using SQL PDF

This document discusses various SQL techniques for grouping and aggregating data, including the GROUP BY clause, ROLLUP operator, CUBE operator, GROUPING function, and GROUPING SETS. It explains how to use these techniques to generate subtotals and grand totals. Group functions like AVG, SUM, MIN, MAX, COUNT, STDDEV and VARIANCE are also covered. The ROLLUP operator is described as creating cumulative aggregates by moving from right to left along the GROUP BY columns. GROUPING SETS allows defining multiple groupings in a single query.

Uploaded by

Ganesh Patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

Grouping and Aggregating Data Using SQL PDF

This document discusses various SQL techniques for grouping and aggregating data, including the GROUP BY clause, ROLLUP operator, CUBE operator, GROUPING function, and GROUPING SETS. It explains how to use these techniques to generate subtotals and grand totals. Group functions like AVG, SUM, MIN, MAX, COUNT, STDDEV and VARIANCE are also covered. The ROLLUP operator is described as creating cumulative aggregates by moving from right to left along the GROUP BY columns. GROUPING SETS allows defining multiple groupings in a single query.

Uploaded by

Ganesh Patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Grouping and Aggregating Data Using SQL

 Review Group functions and group by clause .


 A very nice examples to refresh your knowledge.
 Use the ROLLUP operator.
 Use the CUBE operator.
 Use the GROUPING function.
 Use GROUPING SETS.
 Work with composite columns.
 Use concatenated groupings to generate useful
combinations of groupings.

Prepared By :Khaled AlKhudari 1


Prepared By :Khaled AlKhudari 2
Prepared By :Khaled AlKhudari 3
The most Popular group functions:

 AVG
 SUM
 MIN
 MAX
 COUNT
 STDDEV
 VARIANCE

Prepared By :Khaled AlKhudari 4


Prepared By :Khaled AlKhudari 5
Prepared By :Khaled AlKhudari 6
Prepared By :Khaled AlKhudari 7
Prepared By :Khaled AlKhudari 8
ROLLUP is an extension of the GROUP BY clause.
Use the ROLLUP operation to produce cumulative aggregates,
such as Grand totals & subtotals.

The ROLLUP operator creates groupings by moving


in one direction,
from right to left, along the list
of columns specified in the GROUP BY clause.
It then applies the aggregate function to these groupings.

ROLLUP produces only a fraction of possible subtotal combinations


Prepared By :Khaled AlKhudari 9
Group by rollup (col1);
We will have 2 types of totals
1. The total coming from the group statement
2. The Grand total

Group by rollup (col1, col2);


We will have 3 types of totals N+1
1. The total coming from the group statement
2. The subtotal for col1
3. The grand total

Prepared By :Khaled AlKhudari 10


Group by rollup (col1, col2, col3);
We will have 4 types of totals
1. The total coming from the group statement
2. The subtotal for col1 , col2
3. The subtotal total for col1
4. The grand total N+1
Group by rollup (col1, col2, col3, col4);
We will have 5 types of totals
1. The total coming from the group statement
2. The subtotal for col1 , col2,col3
3. The subtotal total for col1,col2
4. The subtotal total for col1
5. The grand total Grouping and Aggregating Data Using SQL 6.sql

Prepared By :Khaled AlKhudari 11


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 the performance benefit.

Prepared By :Khaled AlKhudari 12

You might also like