Sie sind auf Seite 1von 3

http://www.oracle.com/technetwork/issue-archive/2010/o40asktom-094550.html http://docs.oracle.com/cd/E14072_01/server.112/e10810/analysis.htm#BCFHHHHF http://searchoracle.techtarget.com/feature/Query-Tuning-for-Developer-Beginner-D BAs Book: Oracle SQL Tricks and Workarounds http://www.scribd.com/doc/61447469/Oracle-SQL-Tricks-and-Workarounds http://guyharrison.squarespace.com/blog/2009/7/27/pivot-performance-in-11g.

html Pivoting and UnPivoting: Convert Rows into Columns UNPIVOT: Converts columns into rows

We use Multiple Decode/Case Statements for Pivoting. PIVOT [ Aggregate_Function ] as [ Alias ] FOR Column List in [ SubQuery ] [ SubQuery makes up for the Column Names ]

The Basic Syntax is: SELECT .... FROM <table-expr> ...( It can be Table Name, Table View) PIVOT ( aggregate-function(<column>) FOR <pivot-column> IN (<value1>, <value2>,..., <valuen>) ) AS <alias> WHERE .....

Query1: SELECT * FROM (SELECT AWARD_ID, CALENDAR_YEAR FROM RAW_AWARD_FACT, RAW_TIME_DIM WHERE RAW_AWARD_FACT.GL_DATE_ID=RAW_TIME_DIM.RAW_DAY_ID AND RAW_AWARD_ID=38875 A ND SOURCE_TYPE='BUDGET') PIVOT ( SUM(BUDGET_AMOUNT) FOR CALENDAR_YEAR IN (2007,2008,2009,2010,2011)) (Only when you have PIVOT XML specified, you can enter SubQuery ) Concept1: The Values in the FOR become Column Names. We can have them as Aliases .

PIVOT ( SUM(BUDGET_AMOUNT) FOR CALENDAR_YEAR IN (YEAR_2007,YEAR_2008,YEAR_2009,Y EAR_2010,YEAR_2011)) Budget_Amount: Invalid Identifier Concept2: Pivot can have only that column which has been referenced in the Selec t List Query above. Query2: SELECT * FROM ( SELECT AWARD_ID, CALENDAR_YEAR,BUDGET_AMOUNT FROM RAW_AWARD_FACT, RAW_TIME_DIM WHERE RAW_AWARD_FACT.GL_DATE_ID=RAW_TIME_DIM.RAW_DAY_ID AND RAW_AWARD_ID=38875 A ND SOURCE_TYPE='BUDGET') PIVOT ( SUM(BUDGET_AMOUNT) FOR CALENDAR_YEAR IN (2007,2008,2009,2010,2011)) This Query works Fine... Query3: SELECT * FROM RAW_AWARD_INSTALLMENT_FACT PIVOT (SUM(DIRECT_COST) FOR AWARD_Status in ('Active','At Risk' , 'On Hold','Clo sed')) The Query returns results...( but grouped by all the columns) SELECT AWARD_ID,DIRECT_COST,AWARD_STATUS FROM RAW_AWARD_INSTALLMENT_FACT PIVOT (SUM(DIRECT_COST) FOR AWARD_Status in ('Active','At Risk' , 'On Hold','Clo sed')) ..It Failed..Why?? Misconception: We don't have columns Direct_Cost, Award Status We didn't follow Syntax: The Right Syntax is: SELECT * FROM ( SELECT AWARD_ID,DIRECT_COST,AWARD_STATUS FROM RAW_AWARD_INSTALLMENT_FACT) PIVOT (SUM(DIRECT_COST) FOR AWARD_Status in ('Active','At Risk' , 'On Hold','Clo sed')) SELECT AWARD_ID,ACT,RISK,HOLD,CLOSED FROM RAW_AWARD_INSTALLMENT_FACT PIVOT (SUM(DIRECT_COST) FOR AWARD_Status in ('Active' AS ACT,'At Risk' AS RISK , 'On Hold' AS HOLD,'Closed' AS CLOSED)) Concept3: The PIVOT clause includes SUM(DIRECT_COST) to compute the aggregate SUM(DIRECT_C OST) grouping implicitly

by the remaining columns (AWARD_ID,AWARD_STATUS)

Query4: SELECT award_id FROM RAW_AWARD_INSTALLMENT_FACT PIVOT (SUM(DIRECT_COST) FOR AWARD_Status in ('Active','At Risk' , 'On Hold','Clo sed'))... Query worked but didn't give results The Query should be: SELECT AWARD_ID,ACTIVE FROM RAW_AWARD_INSTALLMENT_FACT PIVOT (SUM(DIRECT_COST) FOR AWARD_Status in ('Active' active,'At Risk' , 'On Ho ld','Closed')) Query5: Get Budget Amounts for 5 Years

SELECT * FROM ( SELECT AWARD_ID, CALENDAR_YEAR,BUDGET_AMOUNT FROM RAW_AWARD_FACT, RAW_TIME_DIM WHERE RAW_AWARD_FACT.GL_DATE_ID=RAW_TIME_DIM.RAW_DAY_ID AND SOURCE_TYPE='BUDGET ') PIVOT ( SUM(BUDGET_AMOUNT) FOR CALENDAR_YEAR IN (2007,2008,2009,2010,2011)) Query 6: Specify SubQuery in FOR IN CLAUSE... The XML keyword permits the pivot_in_clause to contain either a subquery or the wildcard keyword ANY. A subquery is used only in conjunction with the XML keyword. Instead of multiple columns specified in the pivot_in_clause, the subquery produ ces a single XML string column Query7: UNPIVOT works as Normalizer

Das könnte Ihnen auch gefallen