我有下表
Col_1 Col_2 Col_3 Date
0.1 0.2 0.3 2013-08-13
0.4 0.5 0.6 2013-08-12
我想要以下内容:
Date New Column Value
2013-08-13 Col_1 0.1
2013-08-13 Col_2 0.2
2013-08-13 Col_3 0.3
2013-08-12 Col_1 0.4
2013-08-12 Col_2 0.5
2013-08-12 Col_3 0.6
所以,我不仅要转置表格,还要添加一个新列,告诉我列的名称来自哪个列。我试过做一个UNION ..但它不起作用:(
谢谢!
答案 0 :(得分:2)
你可以这样:
INSERT INTO NEW_TABLE_NAME (Date, `New Column`, Value)
SELECT DATE, 'Col_1', Col_1
FROM OLD_TABLE_NAME
UNION ALL
INSERT INTO NEW_TABLE_NAME (Date, `New Column`, Value)
SELECT DATE, 'Col_2', Col_2
FROM OLD_TABLE_NAME
UNION ALL
INSERT INTO NEW_TABLE_NAME (Date, `New Column`, Value)
SELECT DATE, 'Col_3', Col_3
FROM OLD_TABLE_NAME;