如何将列转换为行

时间:2014-11-19 21:44:13

标签: sql oracle

我有一张下表: 表A列:

key col1,key col2,key col3,Non-key col1, Non-key col2,Non-key col3
01,01,01,A,B,C

我想转换此表格如下所示:

表B:

key col1,key col2,key col3, Non-key name, Non-key val
01,01,01,Non-key col1,A
01,01,01,Non-key col2,B
01,01,01,Non-key col3,C

如何将表A转换为表B?

1 个答案:

答案 0 :(得分:0)

怎么样:

SELECT * FROM
  (SELECT KEY_COL1, KEY_COL2, KEY_COL3, NON_KEY_COL1, NON_KEY_COL1 AS DATA
     FROM TABLE_A
   UNION ALL
   SELECT KEY_COL1, KEY_COL2, KEY_COL3, NON_KEY_COL1, NON_KEY_COL2 AS DATA
     FROM TABLE_A
   UNION ALL
   SELECT KEY_COL1, KEY_COL2, KEY_COL3, NON_KEY_COL1, NON_KEY_COL3 AS DATA
     FROM TABLE_A)
  ORDER BY KEY_COL1, KEY_COL2, KEY_COL3, NON_KEY_COL1, DATA;

SQLFiddle here

分享并享受。