首页 新闻 会员 周边

求一个sql 语句 行列转换

0
悬赏园豆:5 [已解决问题] 解决于 2013-04-27 09:05

先有表如下(同一币种收或付只有一条记录),

流水号  收/付 币种 金额

1     收    USD   100

1     付    USD 20

1    收     RMB  200

2    付    RUB  100

 

现在 需要得到下面的表

流水号  USD收 USD付 USD利润(收-付) RMB收 RMB付 。。。

1         100     20      80                    200     0

2       。。。。

Blues T的主页 Blues T | 初学一级 | 园豆:11
提问于:2013-04-26 16:17
< >
分享
最佳答案
0

具体语句我我就不写了,你参考这个

select custid, sum(case when YEAR(orderdate)=2002 then qty end) as [2002], sum(case when YEAR(orderdate)=2003 then qty end) as [2003], sum(case when YEAR(orderdate)=2004 then qty end) as [2004] from orders group by custid

select * from ( select custid,YEAR(orderdate) as years,qty from orders) as ord pivot(sum(qty) for years in([2003],[2004],[2005])) as p

收获园豆:5
不负春光,努力生长 | 小虾三级 |园豆:1382 | 2013-04-26 22:43
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册