【发布时间】:2017-03-07 07:51:25
【问题描述】:
很长一段时间以来,我一直在寻找解决此问题的方法。但是,找不到。
我有一张如下表:
Month Col_1 Col_2 Col_3 Col_4 Col_5
---------------------------------------------
Jan NULL NULL 1 1 1
我想取消透视此表,以便与字段名(Col_1、Col2 等)上的另一个表连接。
我的查询:
select Month,Name,value from
TableName
unpivot
(
Value
for Name in (Col_1,Col_2,Col_3,Col_4,Col_5)
) u
当前结果:
这给了我没有NULL 值如下:
Month Name Value
-----------------------
Jan Col_3 1
Jan Col_4 1
Jan Col_5 1
预期结果:
我希望NULLs 包含在结果中。
Month Name Value
-----------------------
Jan Col_1 NULL
Jan Col_2 NULL
Jan Col_3 1
Jan Col_4 1
Jan Col_5 1
任何帮助将不胜感激。
【问题讨论】:
标签: sql sql-server unpivot