11.07.2015 Views

Oracle Database 11 g - Online Public Access Catalog

Oracle Database 11 g - Online Public Access Catalog

Oracle Database 11 g - Online Public Access Catalog

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 12 ■ DATA WAREHOUSING 533The classic examples of using the pivot operation are monthly sales reports, quarterlysales reports, summarized marketing analysis data, and so on. For example, you can take themonthly sales data and pivot the rows so that each month of data presented in a row will nowshow up as a column in a single inline row.In the previous releases of <strong>Oracle</strong>, you could accomplish a pivot operation using casestatements, as shown here:SQL> select video_name,sum(case when month = '01' then quantity_rented else null end) jan,sum(case when month = '02' then quantity_rented else null end) feb,sum(case when month = '03' then quantity_rented else null end) mar,sum(case when month = '04' then quantity_rented else null end) apr,sum(case when month = '05' then quantity_rented else null end) may,sum(case when month = '06' then quantity_rented else null end) jun,sum(case when month = '07' then quantity_rented else null end) jul,sum(case when month = '08' then quantity_rented else null end) aug,sum(case when month = '09' then quantity_rented else null end) sep,sum(case when month = '10' then quantity_rented else null end) oct,sum(case when month = '<strong>11</strong>' then quantity_rented else null end) nov,sum(case when month = '12' then quantity_rented else null end) decfrom (select video_name, month, quantity_rentedfrom video_mstr_vw )group by video_nameorder by 1/This same query can be rewritten with the pivot syntax:select *from (select video_name, month, quantity_rentedfrom video_mstr_vw)pivot (sum(quantity_rented)for month in('01' as jan,'02' as feb,'03' as mar,'04' as apr,'05' as may,'06' as jun,'07' as jul,'08' as aug,'09' as sep,'10' as oct,'<strong>11</strong>' as nov,'12' as dec ))order by video_name desc;The pivot operator is a breeze compared to the old way of writing the same code. Thisexample shows that the pivot operation is performed on the month with aggregation functions

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!