我一直在使用Stack Overflow中的示例来尝试理解PIVOT函数,例如; Convert Rows to columns using 'Pivot' in mssql
到目前为止,我已经提出以下示例;
CREATE table #rawTable (id int, header varchar(20), visitId int, performedWhen DateTime, recordedValue varchar(20))
INSERT INTO #rawTable (id, header, visitId, performedWhen, recordedValue) VALUES (10,'Blood Pressure', 1,'1 Jan 2014 10:10','110/85')
INSERT INTO #rawTable (id, header, visitId, performedWhen, recordedValue) VALUES (11,'Heart Rate', 1,'1 Jan 2014 10:10','75')
INSERT INTO #rawTable (id, header, visitId, performedWhen, recordedValue) VALUES (12,'Temperature', 1,'1 Jan 2014 10:10','36.9')
INSERT INTO #rawTable (id, header, visitId, performedWhen, recordedValue) VALUES (10,'Blood Pressure', 2,'1 Jan 2014 14:35','120/70')
INSERT INTO #rawTable (id, header, visitId, performedWhen, recordedValue) VALUES (10,'Blood Pressure', 3,'2 Jan 2014','110/80')
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((select ',' + QUOTENAME(convert(varchar(17), performedWhen, 113))
from #rawTable
group by performedWhen
order by performedWhen
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'select header, ' + @cols + ' from
(
select id, header, visitId, performedWhen, recordedValue
from #rawTable
) x
pivot
(
max(recordedValue)
for performedWhen in (' + @cols + ')
) p'
EXEC sp_executesql @query
DROP table #rawTable
这会产生以下结果;
Header 01 Jan 2014 10:10 01 Jan 2014 14:35 02 Jan 2014 00:00
---------------------------------------------------------------------------
Blood Pressure 110/85 NULL NULL
Blood Pressure NULL 120/70 NULL
Blood Pressure NULL NULL 110/80
Heart Rate 75 NULL NULL NULL
Temperature 36.9 NULL NULL NULL
我真正需要的是这个结果(即)ALl在一行上的'血压'值;
Header 01 Jan 2014 10:10 01 Jan 2014 14:35 02 Jan 2014 00:00
---------------------------------------------------------------------------
Blood Pressure 110/85 120/70 110/80
Heart Rate 75 NULL NULL
Temperature 36.9 NULL NULL
我不是在寻找答案,只是一个指向正确方向的线索,因为我无法理解这一点。 :)
SQL Server版本是2012 Enterprise。
答案 0 :(得分:1)
PIVOT函数automatically执行类似于select子句中group by
的操作,如果您不需要visitId
字段,只需更改查询:
set @query = 'select header, ' + @cols + ' from
(
select id, header, performedWhen, recordedValue
from #rawTable
) x
pivot
(
max(recordedValue)
for performedWhen in (' + @cols + ')
) p'
答案 1 :(得分:0)
尝试此查询,我刚从select查询中删除了visitid
CREATE table #rawTable (id int, header varchar(20), visitId int, performedWhen DateTime, recordedValue varchar(20))
INSERT INTO #rawTable (id, header, visitId, performedWhen, recordedValue) VALUES (10,'Blood Pressure', 1,'1 Jan 2014 10:10','110/85')
INSERT INTO #rawTable (id, header, visitId, performedWhen, recordedValue) VALUES (11,'Heart Rate', 1,'1 Jan 2014 10:10','75')
INSERT INTO #rawTable (id, header, visitId, performedWhen, recordedValue) VALUES (12,'Temperature', 1,'1 Jan 2014 10:10','36.9')
INSERT INTO #rawTable (id, header, visitId, performedWhen, recordedValue) VALUES (10,'Blood Pressure', 2,'1 Jan 2014 14:35','120/70')
INSERT INTO #rawTable (id, header, visitId, performedWhen, recordedValue) VALUES (10,'Blood Pressure', 3,'2 Jan 2014','110/80')
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((select ',' + QUOTENAME(convert(varchar(17), performedWhen, 113))
from #rawTable
group by performedWhen
order by performedWhen
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'select header, ' + @cols + ' from
(
select id, header, performedWhen, recordedValue
from #rawTable
) x
pivot
(
max(recordedValue)
for performedWhen in (' + @cols + ')
) p'
EXEC sp_executesql @query
print @query
DROP table #rawTable