1.行转列:pivot 、case when 或 decode
**informatica normalizer transformation也可以实现行列转换**
pivot(聚合函数 for 列名 in(类型)): pivot ( sum ( planqty ) for plantype in ( 'in', 'out' ) ) --有聚合函数
unpivot.:unpivot (planqty for plantype in (inqty, outqty))
1.0 创建测试表格
CREATE TABLE XXXMGR.FAB_BSFACTORYDAILYPLAN
(
FACTORYNAME VARCHAR2 (40 BYTE),
PRODUCTSPECNAME VARCHAR2 (40 BYTE),
PRODUCTTYPE VARCHAR2 (40 BYTE),
PLANDATE VARCHAR2 (40 BYTE),
PLANTYPE VARCHAR2 (40 BYTE),
PLANQTY VARCHAR2 (40 BYTE),
LASTEVENTUSER VARCHAR2 (40 BYTE),
LASTEVENTTIME DATE,
LASTEVENTNAME VARCHAR2 (40 BYTE)
)
TABLESPACE ODS_XXX_DAT;
CREATE UNIQUE INDEX XXXMGR.BSFACTORYDAILYPLAN_PK ON XXXMGR.FAB_BSFACTORYDAILYPLAN
(FACTORYNAME, PRODUCTSPECNAME, PRODUCTTYPE, PLANDATE, PLANTYPE)
TABLESPACE ODS_XXX_IDX;
ALTER TABLE XXXMGR.FAB_BSFACTORYDAILYPLAN ADD (
CONSTRAINT BSFACTORYDAILYPLAN_PK
PRIMARY KEY
(FACTORYNAME, PRODUCTSPECNAME, PRODUCTTYPE, PLANDATE, PLANTYPE)
USING INDEX XXXMGR.BSFACTORYDAILYPLAN_PK
ENABLE VALIDATE);
1.1 行转列:pivot pivot ( sum ( planqty ) for plantype in ( 'in', 'out' ) ) --in、out数量转为列
SELECT * FROM
(SELECT FACTORYNAME
,PRODUCTSPECNAME
,PRODUCTTYPE
,PLANDATE
,PLANTYPE
,PLANQTY
FROM FAB_BSFACTORYDAILYPLAN)
PIVOT (SUM (PLANQTY) FOR PLANTYPE IN ('IN' INQTY, 'OUT' OUTQTY))
1.2 行转列:case when 或 decode (更繁琐)
--CASE WHEN
SELECT FACTORYNAME
,PRODUCTSPECNAME
,PRODUCTTYPE
,PLANDATE
,CASE WHEN PLANTYPE = 'IN' THEN PLANQTY END INQTY
,CASE WHEN PLANTYPE = 'OUT' THEN PLANQTY END OUTQTY
FROM FAB_BSFACTORYDAILYPLAN
--DECODE
SELECT FACTORYNAME
,PRODUCTSPECNAME
,PRODUCTTYPE
,PLANDATE
,DECODE (PLANTYPE,'IN',PLANQTY) INQTY
,DECODE (PLANTYPE,'OUT',PLANQTY) OUTQTY
--Rule5816:Use CASE construct rather than DECODE
FROM FAB_BSFACTORYDAILYPLAN
******************************************************************************************************************************************************
2. 列转行:unpivot
unpivot (planqty for plantype in (inqty, outqty))
2.0 创建测试表格
create table test0 (