我有下表:
oGroup oDate oValue
--------------------------------
A 2014-01-01 20
A 2014-01-02 30
B 2014-01-01 5
B 2014-01-02 15
C 2014-01-01 40
C 2014-01-02 60
我希望得到以下结果:
oGroup 2014-01-01 2014-01-02
----------------------------------
A 20 30
B 5 15
C 40 60
如何在SQL Server 2008中实现此目的?
谢谢。
答案 0 :(得分:4)
了解Pivot
select * from piv
pivot
(
min(oValue)
for oDate in([2014-01-01],[2014-01-02])
)as pivv;
使用Dynamic sql
declare @query nvarchar(max)
declare @cols nvarchar(max)
select @cols=stuff((select distinct ','+QUOTENAME(oDate) from piv for xml path(''),TYPE).value('.','nvarchar(max)'),1,1,'')
select @query='select * from piv
pivot
(
min(oValue)
for oDate in(' + @cols + ')
)as pivv;'
exec (@query)
答案 1 :(得分:2)
您可以使用dynamic crosstab:
DECLARE
@sql1 VARCHAR(4000) = '',
@sql2 VARCHAR(4000) = '',
@sql3 VARCHAR(4000) = ''
SELECT @sql1 =
'SELECT
oGroup' + CHAR(10)
SELECT @sql2 = @sql2 +
' ,MAX(CASE WHEN oDate = CAST(''' + CONVERT(VARCHAR(10), oDate, 112) + ''' AS DATE) THEN oValue END) AS [' + CONVERT(VARCHAR(10), oDate, 120) +']' + CHAR(10)
FROM(
SELECT DISTINCT oDate FROM SampleData
)t
ORDER BY oDate
SELECT @sql3 =
'FROM SampleData
GROUP BY oGroup
ORDER BY oGroup'
PRINT(@sql1 + @sql2 +@sql3)
EXEC (@sql1 + @sql2 +@sql3)
这是PRINT
输出:
SELECT
oGroup
,MAX(CASE WHEN oDate = CAST('20140101' AS DATE) THEN oValue END) AS [2014-01-01]
,MAX(CASE WHEN oDate = CAST('20140102' AS DATE) THEN oValue END) AS [2014-01-02]
FROM SampleData
GROUP BY oGroup
ORDER BY oGroup
答案 2 :(得分:0)
试试这个
我不确定,我不会尝试这个
SELECT a.ogroup,
a.ovalue AS '2014-01-01',
b.ovalue AS '2014-01-02'
FROM (SELECT *
FROM table
WHERE odate = '2014-01-01') AS a
JOIN (SELECT *
FROM table
WHERE odate = '2014-01-02') AS b
ON b.ogroup = a.ogroup