以前在做DBA时在DB里写过行转列,列转行的CODE.这两天做了一下测试用INFORMATICA来实现行列互换的功能。
列转行的SQL实现
ENV: RMDB
TABLE:SALES
STORENAME QUARTER1 QUARTER2 QUARTER3 QUARTER4
STORE1 100 300 500 700
STORE2 200 400 600 800
SOLUTION:
SELECT STORENAME,QUARTER1 AS SALES, 1 AS QUARTER
FROM SALES
GROUP BY STORENAME,QUARTER1
UNION
SELECT STORENAME,QUARTER2 AS SALES, 2 AS QUARTER
FROM SALES
GROUP BY STORENAME,QUARTER2
UNION
SELECT STORENAME,QUARTER3 AS SALES, 3 AS QUARTER
FROM SALES
GROUP BY STORENAME,QUARTER3
UNION
SELECT STORENAME,QUARTER4 AS SALES, 4 AS QUARTER
FROM SALES
GROUP BY STORENAME,QUARTER4
列转行的INFORMATICA实现
1 SOURCE TABLES导入MAPING
2 SQLQUALIFER读取数据
3 NORMALIZER实现对应的列行转换,在NORMALIZER属性里设置STORENAME,QUARTER DATA(为LEVEL 1,每季度的销售值SALES为LEVEL2. QUARTER DATA的OCCURS值设为4就可实现列行转换
二 行转列的SQL实现
ENV:
createtablesales2(storenamevarchar(20),
salesnumber(9),quarternumber(9))
select*fromsales2
SOLUTION:
SELECTSTORENAME,
MAX(CASEWHENQUARTER=1THENSALESELSE0END)QUARTER1,
MAX(CASEWHENQUARTER=2THENSALESELSE0END)QUARTER2,
MAX(CASEWHENQUARTER=3THENSALESELSE0END)QUARTER3,
MAX(CASEWHENQUARTER=4THENSALESELSE0END)QUARTER4
FROMSALES2
GROUPBYSTORENAME
ORDERBYSTORENAME
INFORMATICA实现
1 导入源表SALES2
2 SQLQULIFER读取数据
3 用EXPERSSION TRANSFORMATION来判断QUARTER值来取SALES值
4 用聚合函数来取出判断后的最大值,即真正的SALES值
5导入到目标表或目标文件后即实现了行转列的功能。