【Teradata SQL】行列轉換函數PIVOT和UNPIVOT、TD_UNPIVOT


1.行轉列函數PIVOT

Pivot是用於將行轉換為列的關系運算符。該函數對於報告用途很有用,因為它允許您聚合和旋轉數據以創建易於讀取的表。在SELECT語句的FROM子句中指定PIVOT運算符。對於可以用包含透視運算符的select查詢指定的其他子句沒有限制。

(1)語法

 (2)實例

#數據准備
CREATE
TABLE star1( country VARCHAR(20) ,state VARCHAR(10) , yr INTEGER ,qtr VARCHAR(3) ,sales INTEGER ,cogs INTEGER ); insert into star1 values('USA','CA',2001,'Q1',30,15); insert into star1 values('Canada','ON',2001,'Q2', 10, 0); insert into star1 values('Canada','BC',2001,'Q3', 10 ,0); insert into star1 values('USA','NY',2001,'Q1',45, 25); insert into star1 values('USA','CA',2001,'Q2', 50 ,20); SELECT * FROM star1;

單個轉換維度(Quarter)實例,將Q1、Q2、Q3季度的sales和cogs值轉換為列,轉換效果如下:

#寫法一
SELECT *
FROM star1 PIVOT (
SUM(sales) as ss1, SUM(cogs) as sc FOR
qtr
IN ('Q1' AS
Quarter1,
'Q2' AS Quarter2,
'Q3' AS Quarter3)
)Tmp;

#寫法二 SELECT country, state,
SUM(case when yr = 2001 and qtr = 'Q1' then sales end) as "2001_q1_ss",
SUM(case when yr = 2001 and qtr = 'Q2' then sales end) as "2001_ q2_ ss",
SUM(case when yr = 2001 and qtr = 'Q3' then sales end) as "2001_q3_ss",
SUM(case when yr = 2001 and qtr = 'Q1' then cogs end) as "2001_ q1_sc",
SUM(case when yr = 2001 and qtr = 'Q2' then cogs end) as "2001_q2_sc",
SUM(case when yr = 2001 and qtr = 'Q3' then cogs end) as "2001_q3_sc"
FROM star1
GROUP BY country, state;

兩個轉換維度(year、Quarter)實例,將2001年Q1、Q2、Q3季度的sales和cogs值轉換為列,轉換效果如下:

#寫法一(默認按照聚合字段sales、cogs和 FOR列表字段yr、qtr以外的表中字段進行分組,本例中按照country、state字段分組)
SELECT *
FROM star1 PIVOT (
SUM(sales) AS ss1, SUM(cogs) AS sc FOR 
(yr, qtr)
IN ((2001, 'Q1'),
(2001, 'Q2'),
(2001, 'Q3'))
)Tmp;

#寫法二 SELECT country, state,
SUM(case when yr = 2001 and qtr = 'Q1' then sales end) as "2001_q1_ss",
SUM(case when yr = 2001 and qtr = 'Q2' then sales end) as "2001_ q2_ ss",
SUM(case when yr = 2001 and qtr = 'Q3' then sales end) as "2001_q3_ss",
SUM(case when yr = 2001 and qtr = 'Q1' then cogs end) as "2001_ q1_sc",
SUM(case when yr = 2001 and qtr = 'Q2' then cogs end) as "2001_q2_sc",
SUM(case when yr = 2001 and qtr = 'Q3' then cogs end) as "2001_q3_sc"
FROM star1
GROUP BY country, state;

先行轉列,后列轉行

CREATE TABLE t1 (
place CHAR(5)
, sales1 INTEGER
, sales2 INTEGER
,sales3 INTEGER
, sales4 INTEGER
, sales5 INTEGER)
PRIMARY INDEX ( place );


insert  into t1 values('Hyd' ,110 ,100 ,1000 ,1100, 500);
insert  into t1 values('Che', 120 ,200, 2000, 1200, 600);
insert  into t1 values('Kol' ,150 ,500, 5000, 1500, 900 );
insert  into t1 values('Mee', 140, 400, 4000 ,1400 ,800);
insert  into t1 values('Pun', 130 ,300, 3000 ,1300, 700);

SELECT * from (
 SELECT * from t1
 UNPIVOT(
      saleval
      for sales in (sales1, sales2, sales3,sales4, sales5)
  )dt1
)dt2
PIVOT(
SUM(saleval)
for place in ('hyd','Che','pun','mee','kol')
)dt3;

2.列轉行函數UNPIVOT

UNPIVOT是透視操作的反向操作。它提供了一種將列轉換為行的機制。unpivot功能先前是通過td_unpivot表運算符引入的。此功能引入語法以支持SELECT語句的FROM子句中的UNPIVOT運算符。
注:UNPIVOT在內部調用td_UNPIVOT表運算符。您仍然可以獨立於unpivot使用td_unpivot。

(1)語法

(2)實例

#數據准備
CREATE
TABLE star1p( country VARCHAR(20) ,state VARCHAR(20) ,Q101Sales INTEGER ,Q201Sales INTEGER ,Q301Sales INTEGER ,Q101Cogs INTEGER ,Q201Cogs INTEGER ,Q301Cogs INTEGER ); insert into star1p values('Canada','ON',NULL, 10 ,NULL, NULL, 0, NULL); insert into star1p values('Canada','BC', NULL, NULL ,10, NULL, NULL, 0); insert into star1p values('USA','NY', 45, NULL, NULL, 25 ,NULL, NULL); insert into star1p values('USA','CA', 30 ,50, NULL, 15, 20, NULL); SELECT * FROM star1p;

將Q101、Q201、Q301的sales和cogs列數據,轉換為行數據

SELECT *
FROM star1p UNPIVOT (
   (sales,cogs) 
   FOR yr_qtr IN (
       (Q101Sales, Q101Cogs) AS 'Q101',
       (Q201Sales, Q201Cogs) AS 'Q201',
       (Q301Sales, Q301Cogs) AS 'Q301'
    )
) Tmp;

 將Q101、Q201、Q301的sales和cogs列數據,轉換為行數據----包含NULL值

SELECT *
FROM star1p UNPIVOT INCLUDE NULLS (
    (sales,cogs) 
    FOR yr_qtr IN(
      (Q101Sales, Q101Cogs) AS 'Q101'
      , (Q201Sales, Q201Cogs) AS 'Q201'
      , (Q301Sales,Q301Cogs) AS 'Q301'
    )
) Tmp;

將Q101、Q201、Q301的sales和cogs列數據,轉換為行數據----不含NULL值

SELECT *
FROM star1p UNPIVOT EXCLUDE NULLS (
    (sales, cogs) 
    FOR yr_qtr IN(
        (Q101Sales, Q101Cogs) AS 'Q101'
        , (Q201Sales, Q201Cogs) AS 'Q201'
        , (Q301Sales,Q301Cogs) AS 'Q301'
    )
) Tmp;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM