将数据列转换为行的过程称为 UNPIVOT。有几种方法可以取消透视数据:
您可以使用 UNION ALL 查询:
select 'Photonics' name, dt + cast('12:15 AM' as datetime) timestamp, [12:15 AM] as value
from Photonics
union all
select 'Photonics' name, dt + cast('12:30 AM' as datetime) timestamp, [12:30 AM] as value
from Photonics
union all
select 'Photonics' name, dt + cast('12:45 AM' as datetime) timestamp, [12:45 AM] as value
from Photonics;
请参阅SQL Fiddle with Demo。
您可以在 SQL Server 2005+ 中使用 UNPIVOT 函数:
select 'Photonics' name,
timestamp = dt + cast(timestamp as datetime),
value
from photonics p
unpivot
(
value
for timestamp in ([12:15 AM], [12:30 AM], [12:45 AM], [1:00 AM], [1:15 AM])
) unpiv;
请参阅SQL Fiddle with Demo。
您可以在 SQL Server 2008+ 中将 CROSS APPLY 与 Values 一起使用:
select 'Photonics' name,
timestamp = dt + cast(timestamp as datetime),
value
from photonics p
cross apply
(
values
('12:15 AM', [12:15 AM]),
('12:30 AM', [12:30 AM]),
('12:45 AM', [12:45 AM]),
('1:00 AM', [1:00 AM]),
('1:15 AM', [1:15 AM])
) c (timestamp, value);
请参阅带有演示的 SQL Fiddle