日期:2014-05-17  浏览次数:20673 次

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 dual
union all
select 'ND','2008' from dual
union all
select 'KTXM','南缘' from dual
union all
select 'GQ','华油' from dual
union all
select 'XMLX','清华' from dual
union all
select '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 T
DW                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');