piviting data

Upload: ravi-kumar-pasupula

Post on 03-Jun-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Piviting Data

    1/3

    PIVOTING DATA

    ------------------------

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

    Te distinct row values become te columns in te output and aggregated column value places itself

    under te appropriate pivoted column. Te 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 te pivoted data into 0)%

    PIVOT1&%A(#$ uses an aggregate function on one of te column of te table. Tis is te data

    wic places itself against te pivoted column accordingl!. PIVOT1O1&%A(#$ and PIVOT1IN1&%A(#$ specif! a column and its distinct values

    wic are to be pivoted. In a transposed report5 te distinct values of te pivoted column appear as

    te eader in te output. /ot are mandator! clauses5 so distinct values of te column must be in

    and.

    A retail firm maintains te trac6 of customer sales in tree products *Product1A5

    Product1/5 Product1&+ for te monts of 7anuar!5 ebruar! and )arc. Te sales data is collected

    for tree privileged customers &85 &9 and &:.

    Te relational table &(#T1#A%$# stores te data for eac customer against eac

    product in eac mont. Te table data is as below

    Now5 we sall pivot te table data to give a new anal!tic dimension. efer te below cases.

    Case 1: Customer sales in each month for each product

    POD(&T1ID column of te &(#T1#A%$# table as been pivoted in te below ;uer!. Now

    observe te beaut! and intelligence of Pivot operator< it retains te positions of remaining columns*i.e. &(#TO)$1ID and )ONT3+ and formats te sales data in accordance wit te pivoted

    column. Also note tat te POD(&T1ID is no more a column but its distinct values i.e. Prod A5

    http://www.dbanotes.com/wp/wp-content/uploads/2011/09/Oracle-11g-Pivot-functions9.jpg
  • 8/12/2019 Piviting Data

    2/3

    Prod /5

    Prod & are transposed as te column eader in te ;uer! output.

    #$%$&T = O) &(#T1#A%$#

    PIVOT

    *

    #()*#A%$#+O POD(&T1ID

    IN *>Prod A?5 >Prod /?5 >Prod &?+

    +

    In te above ;uer!5 note te aggregated function5 O clause and IN clause of Pivot operator.

    Case 2: Customer sales for each product in each month

    Now5 canging te angle of perception b! replacing POD(&T1ID wit te )ONT3 at te eader

    level. Distinct values of )ONT3 appear as column in te ;uer! output.

    #$%$&T = O) &(#T1#A%$#

    PIVOT

    *

    #()*#A%$#+

    O )ONT3

    IN *>7an? as @7anuar!5 >eb? as @ebruar!5 >)ar? as @)arc+

    +

    In te ;uer!5 note tat if te value*s+ of te pivoted column ad to ave a customiBed title for better

    readabilit!5 te same can be specified using @A# 6e!word. In te &(#T1#A%$# table5 abbreviated

    values of te )ONT3 column ave been replaced wit teir complete names.

    &$AT$ TA/%$ pivot1test *

    id N()/$5

    customer1id N()/$5 product1code VA&3A9*C+5

    ;uantit! N()/$

  • 8/12/2019 Piviting Data

    3/3

    +