Hi,
Today I am explaining you about two simple functions;but powerful as it can be used for data warehousing application. Those are mentioned below.
- create table sales_sample ( sale_region varchar2(30), sale_year number, sale_month varchar2(20), sale_product_code varchar2(30), quantity number )
I am populating the table with the following values.
- insert into sales_sample values('North America',2014,'Jan','Iphone',100);
- insert into sales_sample values('North America',2014,'Jan','Samsung',200);
- insert into sales_sample values('North America',2014,'Jan','Nexus',300);
- insert into sales_sample values('Asia Pacific',2014,'Jan','Iphone',510);
- insert into sales_sample values('Asia Pacific',2014,'Jan','Samsung',220);
- insert into sales_sample values('Asia Pacific',2014,'Jan','Nexus',320);
- insert into sales_sample values('North America',2014,'Feb','Iphone',11);
- insert into sales_sample values('North America',2014,'Feb','Samsung',22);
- insert into sales_sample values('North America',2014,'Feb','Nexus',33);
- insert into sales_sample values('Asia Pacific',2014,'Feb','Iphone',44);
- insert into sales_sample values('Asia Pacific',2014,'Feb','Samsung',55);
- insert into sales_sample values('Asia Pacific',2014,'Feb','Nexus',66);
- SQL>select * from sales_sample;
- SALE_REGION SALE_YEAR SALE_MONTH SALE_PRODUCT_CODE QUANTITY
- --------------------------------------------------------------------------------------------
- North America 2014 Jan Iphone 100
- North America 2014 Jan Samsung 200
- North America 2014 Jan Nexus 300
- North America 2014 Feb Iphone 11
- North America 2014 Feb Samsung 22
- North America 2014 Feb Nexus 33
- Pacific 2014 Jan Iphone 510
- Pacific 2014 Jan Samsung 220
- Pacific 2014 Jan Nexus 320
- Pacific 2014 Feb Iphone 44
- Pacific 2014 Feb Samsung 55
- Pacific 2014 Feb Nexus 66
- -----------------------------------------------------------------------------------------
Now apply the pivot function on the data set; the syntax is mentioned below
Pivot Function which will give region,year and month level aggregates of totla quantity of products sold.
Pivot Function which will give region,year and month level aggregates of totla quantity of products sold.
- select * from
- (
- select sale_region, SALE_YEAR, sale_month,sale_product_code,quantity from sales_sample ) PIVOT
- (
- SUM(quantity)
- FOR sale_product_code IN ('Iphone' AS IPHONE_QUANTITY, 'Samsung' SAMSUNG_QUANTITY,'Nexus' NEXUS_QUANTITY)
- )
- ORDER BY SALE_REGION,sale_month
- Output
- --------
- REGION YEAR MONTH IPHONE_QUANTITY SAMSUMG_QUANITY NEXUS_QUANTITY
- ----------------------------------------------------------------------------------------- ---------------------
- Asia Pacific 2014 Feb 44 55 66
- Asia Pacific 2014 Jan 510 220 320
- North America 2014 Feb 11 22 33
- North America 2014 Jan 100 200 300
- --------------------------------------------------------------------------------------------------------------
UnPivot Example
--------------------
- create table sales_region_year(
- SALE_REGION VARCHAR2(30)
- SALE_YEAR NUMBER
- IPHONE_QUANTITY NUMBER
- SAMSUNG_QUANTITY NUMBER
- NEXUS_QUANTITY NUMBER
- )
- SELECT * FROM SALES_REGION_YEAR
- UNPIVOT INCLUDE NULLS (quantity FOR product_code IN (IPHONE_QUANTITY AS 'Iphone', SAMSUNG_QUANTITY AS 'Samsung', NEXUS_QUANTITY AS 'Nexus'));
- SQL>SELECT * FROM SALES_REGION_YEAR ;
- SALE_REGION SALE_YEAR PRODUCT_CODE QUANTITY
- ------------------------------------------------------------------
- North America 2014 Iphone 111
- North America 2014 Samsung 222
- North America 2014 Nexus 333
- Pacific 2014 Iphone 554
- Pacific 2014 Samsung 275
- Pacific 2014 Nexus 386
- --------------------------------------------------------------------
Hope it is very informative.
No comments:
Post a Comment