开发者

Column Split Sum over a single ID

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


You want to use PIVOT

Look here.

And here: How can i use pivot?

Update

With the new info (Teradata DB), here's the solution:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜