2

Similar to a question I had earlier

Having this table

ID, Year, Revenue 
1, 2009, 10 
1, 2009, 20 
1, 2010, 20 
2, 2009, 5 
2, 2010, 50
2, 2010, 1

Is it possible to make a query that results in something similar to this?

ID 2009 2010
1  30   20        
2  5    51
4

1 回答 1

3

你想用PIVOT

这里

在这里:我如何使用枢轴?

更新

使用新信息(Teradata DB),解决方案如下:

select 
  ID,
  Sum(CASE When Year = 2009 then Revenue ELSE 0 END) as Y2009,
  Sum(CASE When Year = 2010 then Revenue ELSE 0 END) as Y2010
From
  YourTable
Group by ID
于 2011-06-23T20:10:54.437 回答