当前位置: 代码迷 >> Oracle管理 >> oracle行转列,单行!解决思路
  详细解决方案

oracle行转列,单行!解决思路

热度:57   发布时间:2016-04-24 05:25:09.0
oracle行转列,单行!!!
zdmc zdz
--------------
DW 成都
ND 2008
KTXM 南缘
GQ 华油
XMLX 清华
TKRQ 2008-3-18 0:00:00

期望转成:
dw nd ktxm gq xmlx tkrq
----------------------------------
成都 2008 南缘 华油 清华 2008-3-18 0:00:00

------解决方案--------------------
SQL code
with t as(select 'DW' zdmc,'成都' zdz from dualunion allselect 'ND','2008' from dualunion allselect 'KTXM','南缘' from dualunion allselect 'GQ','华油' from dualunion allselect 'XMLX','清华' from dualunion allselect 'TKRQ','2008-3-18 0:00:00' from dual)SELECT max(DECODE(zdmc, 'DW', zdz)) DW,       max(DECODE(zdmc, 'ND', zdz)) ND,       max(DECODE(zdmc, 'KTXM', zdz)) KTXM,       max(DECODE(zdmc, 'GQ', zdz)) GQ,       max(DECODE(zdmc, 'XMLX', zdz)) XMLX,       max(DECODE(zdmc, 'TKRQ', zdz)) TKRQ  FROM TDW                ND                KTXM              GQ                XMLX              TKRQ----------------- ----------------- ----------------- ----------------- ----------------- -----------------成都              2008              南缘              华油              清华              2008-3-18 0:00:00
------解决方案--------------------
楼上最经典的行转列,百度导出都是
------解决方案--------------------
一般做法就是一个一个判断
------解决方案--------------------
测试数据:
SQL code
CREATE TABLE T186(    ZDMC VARCHAR2(20),    ZDZ  VARCHAR2(20));INSERT INTO T186 VALUES('DW', '成都');INSERT INTO T186 VALUES('ND', '2008');INSERT INTO T186 VALUES('KTXM', '南缘');INSERT INTO T186 VALUES('GQ', '华油');INSERT INTO T186 VALUES('XMLX', '清华');INSERT INTO T186 VALUES('TKPQ', '2008-3-18 0:00:00');
  相关解决方案