您将需要使用该PIVOT
函数来转换数据。
如果您的值数量有限Sensor
,则可以对查询进行硬编码:
select *
from
(
select sensor, timestamp, value
from yourtable
) src
pivot
(
max(value)
for sensor in (A, B)
) piv
请参阅带有演示的 SQL Fiddle
但如果值未知,那么您将需要实现动态 SQL:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Sensor)
from yourtable -- table containing Sensor values
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT timestamp, ' + @cols + ' from
(
select sensor, timestamp, value
from yourtable
) x
pivot
(
max(value)
for sensor in (' + @cols + ')
) p '
execute(@query)
请参阅带有演示的 SQL Fiddle
两者都会给出结果:
| TIMESTAMP | A | B |
-------------------------------------
| 2013-12-02 09:00:00 | 10.4 | 15.3 |
| 2013-12-02 10:00:00 | 10.3 | 16.4 |
| 2013-12-02 11:00:00 | 10.1 | 15.4 |
请注意,您将用yourtable
当前查询替换 。
编辑#1,如果要按日期过滤,可以使用以下动态 sql:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX),
@startdate datetime,
@enddate datetime
set @startdate = '2013-12-01'
set @enddate = '2013-12-03'
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Sensor)
from yourtable
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT timestamp, ' + @cols + ' from
(
select sensor, timestamp, value
from yourtable
where timestamp >= '''+convert(varchar(10), @startdate, 120)+'''
and timestamp <= '''+convert(varchar(10), @enddate, 120)+'''
) x
pivot
(
max(value)
for sensor in (' + @cols + ')
) p '
execute(@query)
请参阅带有演示的 SQL Fiddle