【发布时间】:2017-11-16 20:45:30
【问题描述】:
我目前有一张类似这样的表格:
我需要将数据导入新表,但将列标题设置为行和每个行的值。与此类似。
我会使用 Pivot 还是 Unpivot 来执行此操作?我有很多大约 700 行的列需要以这种方式进行转换。
【问题讨论】:
-
您使用什么软件/语言?你尝试了什么?
标签: sql sql-server unpivot
我目前有一张类似这样的表格:
我需要将数据导入新表,但将列标题设置为行和每个行的值。与此类似。
我会使用 Pivot 还是 Unpivot 来执行此操作?我有很多大约 700 行的列需要以这种方式进行转换。
【问题讨论】:
标签: sql sql-server unpivot
你应该选择unpivot,试试下面的查询。一般来说,Pivot 会减少总行数,而Unpivot 会增加行数。
select SN,Property,Value
from tbl
unpivot
(Value for Property in (Property1,Property2,Property3,Property4) ) as tblunpvt
【讨论】:
如果您没有很多列,您可以随时尝试:
SELECT SN, 'Property1' as Property, Property1 as Value from <TABLE>
UNION ALL
SELECT SN, 'Property2' as Property, Property2 as Value from <TABLE>
UNION ALL
SELECT SN, 'Property3' as Property, Property3 as Value from <TABLE>
【讨论】: