0% found this document useful (0 votes)
6 views12 pages

ASQL - Lecture 3

The document outlines Lecture 3 of an Advanced T-SQL course, focusing on advanced aggregation techniques and set operators. Key topics include grouping sets, CUBE, ROLLUP, pivoting and unpivoting data, as well as set operators like UNION, INTERSECT, and EXCEPT. In-class lab practices are included to reinforce the concepts using the AdventureWorksDW2020 database.

Uploaded by

hieulaex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views12 pages

ASQL - Lecture 3

The document outlines Lecture 3 of an Advanced T-SQL course, focusing on advanced aggregation techniques and set operators. Key topics include grouping sets, CUBE, ROLLUP, pivoting and unpivoting data, as well as set operators like UNION, INTERSECT, and EXCEPT. In-class lab practices are included to reinforce the concepts using the AdventureWorksDW2020 database.

Uploaded by

hieulaex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

COURSE ADVANCED T-SQL:

Lecture 3:
Advanced Aggregation and Set operators
OUTLINE

1 ADVANCED AGGR EGAT IO N


GR OUPI NG S E TS
CUBE and RO L L UP
Pi v oti n g and Un p i v oti n g Da t a

2 SET OPERAT O R S
R ev i s e Uni on a n d Un i on a l l
I ntersect
Ex cepts

3 I N CLASS LA B PR ACT I C E S
1
ADVANCED
AGGREGATION
GROUPING SETS
W i t h T - S Q L , y o u c a n d e f i n e m u l t i p l e g r o u p i n g s e t s i n t h e s a m e qu e r y . I n oth e r w or ds , y ou
can use one query to group the data in more than one way.
T - S Q L s u p p o r t s t h r e e c l a u s e s t h a t a l l o w d e f i n e d m u l t i p l e g r ou pi n g s e ts : G R O U P I N G S E T S ,
CUBE, and ROLLUP

GROUPING SETS
GROUPING SETS
In class Lab 1:
Using AdventureWorksDW2020
Calculate either TotalRevenue by EnglishProductSubcategoryName, by EnglishCategoryName
and overall Revenue from dbo.FactInternetSales

Grouping set on
ProductCategory

Empty Grouping,
over all SalesAmount
CUBE AND ROLLUP
CUBE

T h e C U B E c l a u s e a c c e p t s a l i s t o f e x p r e s s i o n s a s i n p u t s a n d de f i n e s a l l pos s i bl e g r ou pi n g
sets that can be generated from the inputs

ROLL UP

T h e R O L L U P c l a u s e i s a l s o a n a b b r e v i a t i o n o f t h e G R O U P I N G S E T S cl a u s e , bu t y ou u s e i t
w h e n t h e r e ’ s a n a t u r a l h i e r a r c h y f o r m e d b y t h e i n p u t e l e m e n ts .

F o r e x a m p l e : a L o c a t i o n h i e r a r c h y : s h ip c o u n t r y > s h ip r e g io n > s h i p c i t y ; a D a t e h i e r a r c h y : y e a r
> quarter > month
GROUPING FUNCTIONS
GROUPING FUNCTION

G R O U P I N G i s u s e d t o d i s t i n g u i s h t h e n u l l v a l u e s t h a t a r e r e tu r n e d by R O L L U P , C U B E or
G R O U P I N G S E T S f r o m s t a n d a r d n u l l v a l u e s . T h e N U L L r e t u r n e d a s th e r e s u l t of a R O L L U P ,
CUBE or GROUPING SETS operation is a special use of NULL.

GROUPING_ID FUNCTION

G R O U P I N G _ I D a c c e p t s t h e l i s t o f g r o u p e d c o l u m n s a s i n p u t s a n d r e tu r n s a n i n te g e r
representing a bitmap
PIVOT AND UNPIVOT
PIVOT

P i v o t i n g i s a t e c h n i q u e t h a t g r o u p s a n d a g g r e g a t e s d a t a , t r a n s i t i o n i n g i t f r om a s ta te of r ow s
t o a s t a t e o f c o l u m n s . I n a l l p i v o t q u e r i e s , y o u n e e d t o i d e n t i f y th r e e e l e m e n ts :
1. What do you want to see on rows?
- > T h i s e l e m e n t i s k n o w n a s t h e o n r o w s , o r g r o u p i n g e l e m e n t.
2. What do you want to see on columns?
- > T h i s e l e m e n t i s k n o w n a s t h e o n c o l s , o r s p r e a d i n g e l e m e n t.
3 . W h a t d o y o u w a n t t o s e e i n t h e i n t e r s e c t i o n o f e a c h d i s ti nct r o w and co l u m n val u e ?
-> This element is known as the data, or aggregation element.
PIVOT AND UNPIVOT

In class Lab 2:
Using AdventureWorksDW2020, table dbo.DimEmployee
Pivot the number of Employee in DepartmentName:
Production, Engineering and Sales for each Gender.
The result should be like this:
PIVOT AND UNPIVOT
UNPIVOT

Unpivoting data can be considered the inverse of pivoting.


W h e n u n p i v o t i n g d a t a , y o u r o t a t e t h e i n p u t d a t a f r o m a s t a t e of col u m n s to a s ta te of r ow s .
You need to identify the three elements involved:
1. The name you want to assign to the target values column
2. The name you want to assign to the target attribute column
3 . T h e s e t o f s o u r c e c o l u m n s t h a t y o u ’r e u n p i v o t i n g

In class Lab 3:
Assign result of Lab 2 as a temp table named #PivotEmp
Then Unpivoting that table
2
SET OPERATORS
SET OPERATORS
UNION AND UNION ALL INTERSECT EXCEPT
The EXCEPT operator performs set
The INTERSECT operator returns
The UNION operator unifies the difference. It returns distinct rows that
only distinct rows that are common
results of the two input queries. appear in the result
to both sets
of the first query but not the second.

? Wha t i s t he d i f f e r e n ce b e twe e n U N I O N a n d U N I ON AL L

You might also like