You are on page 1of 3

PIVOTING DATA -----------------------Pivot operator transposes an aggregated row of a table into a column.

T e distinct row values become t e columns in t e output and aggregated column value places itself under t e appropriate pivoted column. T e s!nta" of Pivot operator is as below. #$%$&T '&O%()N*#+, -.O) 'TA/%$ NA)$, PIVOT '0)%, * pivot1clause pivot1for1clause pivot1in1clause + 23$.$ '&ONDITION#, '0)%, 4 optional clause to convert t e pivoted data into 0)% PIVOT1&%A(#$ uses an aggregate function on one of t e column of t e table. T is is t e data w ic places itself against t e pivoted column accordingl!. PIVOT1-O.1&%A(#$ and PIVOT1IN1&%A(#$ specif! a column and its distinct values w ic are to be pivoted. In a transposed report5 t e distinct values of t e pivoted column appear as t e eader in t e output. /ot are mandator! clauses5 so distinct values of t e column must be in and. A retail firm maintains t e trac6 of customer sales in t ree products *Product1A5 Product1/5 Product1&+ for t e mont s of 7anuar!5 -ebruar! and )arc . T e sales data is collected for t ree privileged customers &85 &9 and &:. T e relational table &(#T1#A%$# stores t e data for eac customer against eac product in eac mont . T e table data is as below

Now5 we s all pivot t e table data to give a new anal!tic dimension. .efer t e below cases.

Case 1: Customer sales in each month for each product


P.OD(&T1ID column of t e &(#T1#A%$# table as been pivoted in t e below ;uer!. Now observe t e beaut! and intelligence of Pivot operator< it retains t e positions of remaining columns *i.e. &(#TO)$.1ID and )ONT3+ and formats t e sales data in accordance wit t e pivoted column. Also note t at t e P.OD(&T1ID is no more a column but its distinct values i.e. Prod A5

Prod /5 Prod & are transposed as t e column eader in t e ;uer! output. #$%$&T = -.O) &(#T1#A%$# PIVOT * #()*#A%$#+ -O. P.OD(&T1ID IN *>Prod A?5 >Prod /?5 >Prod &?+ + In t e above ;uer!5 note t e aggregated function5 -O. clause and IN clause of Pivot operator.

Case 2: Customer sales for each product in each month


Now5 c anging t e angle of perception b! replacing P.OD(&T1ID wit t e )ONT3 at t e eader level. Distinct values of )ONT3 appear as column in t e ;uer! output. #$%$&T = -.O) &(#T1#A%$# PIVOT * #()*#A%$#+ -O. )ONT3 IN *>7an? as @7anuar!A5 >-eb? as @-ebruar!A5 >)ar? as @)arc A+ + In t e ;uer!5 note t at if t e value*s+ of t e pivoted column ad to ave a customiBed title for better readabilit!5 t e same can be specified using @A#A 6e!word. In t e &(#T1#A%$# table5 abbreviated values of t e )ONT3 column ave been replaced wit t eir complete names.

&.$AT$ TA/%$ pivot1test * id N()/$.5 customer1id N()/$.5 product1code VA.&3A.9*C+5 ;uantit! N()/$.

+< IN#$.T INTO pivot1test VA%($# *85 85 DAD5 8E+< IN#$.T INTO pivot1test VA%($# *95 85 D/D5 9E+< IN#$.T INTO pivot1test VA%($# *:5 85 D&D5 :E+< IN#$.T INTO pivot1test VA%($# *F5 95 DAD5 FE+< IN#$.T INTO pivot1test VA%($# *C5 95 D&D5 CE+< IN#$.T INTO pivot1test VA%($# *G5 :5 DAD5 GE+< IN#$.T INTO pivot1test VA%($# *H5 :5 D/D5 HE+< IN#$.T INTO pivot1test VA%($# *I5 :5 D&D5 IE+< IN#$.T INTO pivot1test VA%($# *J5 :5 DDD5 JE+< IN#$.T INTO pivot1test VA%($# *8E5 F5 DAD5 8EE+< &O))IT< =================== T!pe-8 ============================ #$%$&T = -.O) *#$%$&T product1code5 ;uantit! -.O) pivot1test+ PIVOT *#()*;uantit!+ A# sum1;uantit! -O. *product1code+ IN *DAD A# a5 D/D A# b5 D&D A# c++< =================== T!pe-9 ============================== #$%$&T = -.O) *#$%$&T customer1id5 product1code5 ;uantit! -.O) pivot1test+ PIVOT *#()*;uantit!+ A# sum1;uantit! -O. product1code IN *DAD A# a5 D/D A# b5 D&D A# c++ O.D$. /K customer1id< =================== Old )et od*Prior to 88G+ ============== #$%$&T #()*D$&OD$*product1code5 DAD5 ;uantit!5 E++ A# a1sum1;uantit!5 #()*D$&OD$*product1code5 D/D5 ;uantit!5 E++ A# b1sum1;uantit!5 #()*D$&OD$*product1code5 D&D5 ;uantit!5 E++ A# c1sum1;uantit! -.O) pivot1test O.D$. /K customer1id< #$%$&T customer1id5 #()*D$&OD$*product1code5 DAD5 ;uantit!5 E++ A# a1sum1;uantit!5 #()*D$&OD$*product1code5 D/D5 ;uantit!5 E++ A# b1sum1;uantit!5 #()*D$&OD$*product1code5 D&D5 ;uantit!5 E++ A# c1sum1;uantit! -.O) pivot1test G.O(P /K customer1id O.D$. /K customer1id<

You might also like