Вы находитесь на странице: 1из 3

PIVOTING DATA

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

Pivot operator transposes an aggregated row of a table into a column. The distinct row values become the columns in the output and aggregated column value places itself under the appropriate pivoted column. The syntax of Pivot operator is as below.

SELECT [COLUMN(S)] FROM [TABLE NAME] PIVOT [XML] ( pivot_clause pivot_for_clause pivot_in_clause ) WHERE [CONDITIONS]

[XML] – optional clause to convert the pivoted data into XML PIVOT_CLAUSE uses an aggregate function on one of the column of the table. This is the data which places itself against the pivoted column accordingly. PIVOT_FOR_CLAUSE and PIVOT_IN_CLAUSE specify a column and its distinct values which are to be pivoted. In a transposed report, the distinct values of the pivoted column appear as the header in the output. Both are mandatory clauses, so distinct values of the column must be in hand.

A retail firm maintains the track of customer sales in three products (Product_A, Product_B, Product_C) for the months of January, February and March. The sales data is collected for three privileged customers C1, C2 and C3.

The relational table CUST_SALES stores the data for each customer against each product in each month. The table data is as below

each product in each month. The table data is as below Now, we shall pivot the

Now, we shall pivot the table data to give a new analytic dimension. Refer the below cases.

Case 1: Customer sales in each month for each product

PRODUCT_ID column of the CUST_SALES table has been pivoted in the below query. Now observe the beauty and intelligence of Pivot operator; it retains the positions of remaining columns (i.e. CUSTOMER_ID and MONTH) and formats the sales data in accordance with the pivoted column. Also note that the PRODUCT_ID is no more a column but its distinct values i.e. Prod A,

Prod B,

Prod C are transposed as the column header in the query output. SELECT * FROM CUST_SALES PIVOT

(

SUM(SALES) FOR PRODUCT_ID

IN (‘Prod A’, ‘Prod B’, ‘Prod C’)

)

In the above query, note the aggregated function, FOR clause and IN clause of Pivot operator.

function, FOR clause and IN clause of Pivot operator. Case 2: Customer sales for each product

Case 2: Customer sales for each product in each month

Now, changing the angle of perception by replacing PRODUCT_ID with the MONTH at the header level. Distinct values of MONTH appear as column in the query output.

SELECT * FROM CUST_SALES PIVOT

(

SUM(SALES)

FOR MONTH

IN (‘Jan’ as “January”, ‘Feb’ as “February”, ‘Mar’ as “March”)

)

In the query, note that if the value(s) of the pivoted column had to have a customized title for better readability, the same can be specified using “AS” keyword. In the CUST_SALES table, abbreviated values of the MONTH column have been replaced with their complete names.

MONTH column have been replaced with their complete names. CREATE TABLE pivot_test ( id customer_id product_code

CREATE TABLE pivot_test (

id

customer_id

product_code VARCHAR2(5),

quantity

NUMBER,

NUMBER,

NUMBER

);

INSERT INTO pivot_test VALUES (1, 1, 'A', 10); INSERT INTO pivot_test VALUES (2, 1, 'B', 20); INSERT INTO pivot_test VALUES (3, 1, 'C', 30); INSERT INTO pivot_test VALUES (4, 2, 'A', 40); INSERT INTO pivot_test VALUES (5, 2, 'C', 50); INSERT INTO pivot_test VALUES (6, 3, 'A', 60); INSERT INTO pivot_test VALUES (7, 3, 'B', 70); INSERT INTO pivot_test VALUES (8, 3, 'C', 80); INSERT INTO pivot_test VALUES (9, 3, 'D', 90); INSERT INTO pivot_test VALUES (10, 4, 'A', 100); COMMIT;

******************* Type-1 ****************************

SELECT *

FROM

(SELECT product_code, quantity

FROM

pivot_test)

PIVOT (SUM(quantity) AS sum_quantity FOR (product_code) IN ('A' AS a, 'B' AS b, 'C' AS c));

******************* Type-2 ******************************

SELECT *

FROM

(SELECT customer_id, product_code, quantity

FROM

pivot_test)

PIVOT (SUM(quantity) AS sum_quantity FOR product_code IN ('A' AS a, 'B' AS b, 'C' AS c)) ORDER BY customer_id;

******************* Old Method(Prior to 11G) **************

SELECT SUM(DECODE(product_code, 'A', quantity, 0)) AS a_sum_quantity, SUM(DECODE(product_code, 'B', quantity, 0)) AS b_sum_quantity, SUM(DECODE(product_code, 'C', quantity, 0)) AS c_sum_quantity FROM pivot_test ORDER BY customer_id;

SELECT customer_id, SUM(DECODE(product_code, 'A', quantity, 0)) AS a_sum_quantity, SUM(DECODE(product_code, 'B', quantity, 0)) AS b_sum_quantity, SUM(DECODE(product_code, 'C', quantity, 0)) AS c_sum_quantity FROM pivot_test GROUP BY customer_id ORDER BY customer_id;