這種類型的數據轉換被稱爲PIVOT
。從SQL Server 2005開始,有一個函數可以爲你執行這個數據輪換。但是這可以通過許多不同的方式來完成。
您可以使用聚合函數和CASE
到樞數據:
select
name,
max(case when date = '2013-04-01' then city end) [City 04/01/2013],
max(case when date = '2013-05-01' then city end) [City 05/01/2013]
from yourtable
group by name
見SQL Fiddle with Demo
或者你可以使用PIVOT
功能:
select name, [2013-04-01] as [City 04/01/2013], [2013-05-01] as [City 05/01/2013]
from
(
select name, city, date
from yourtable
) src
pivot
(
max(city)
for date in ([2013-04-01], [2013-05-01])
) piv
見SQL Fiddle with Demo 。
這甚至可以多次參加你的桌子上完成:
select d1.name,
d1.city [City 04/01/2013],
d2.city [City 05/01/2013]
from yourtable d1
left join yourtable d2
on d1.name = d2.name
and d2.date = '2013-05-01'
where d1.date = '2013-04-01'
見SQL Fiddle with Demo。
如果您知道要將日期轉換爲列的上述查詢將很有用。但是,如果你有一個未知的列數,那麼你將要使用動態SQL:
DECLARE @cols AS NVARCHAR(MAX),
@colNames AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(convert(char(10), date, 120))
from yourtable
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
select @colNames = STUFF((SELECT distinct ',' + QUOTENAME(convert(char(10), date, 120)) +' as '+ QUOTENAME('City '+convert(char(10), date, 120))
from yourtable
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT name, ' + @colNames + ' from
(
select name,
city,
convert(char(10), date, 120) date
from yourtable
) x
pivot
(
max(city)
for date in (' + @cols + ')
) p '
execute(@query)
見SQL Fiddle with Demo
他們都給出結果:
| NAME | CITY 04/01/2013 | CITY 05/01/2013 |
----------------------------------------------
| Paul | Milan | Berlin |
| Charls | Rome | El Cairo |
| Jim | Tokyo | Milan |
| Justin | San Francisco | Paris |
| Bill | London | Madrid |