先有表如下(同一币种收或付只有一条记录),
流水号 收/付 币种 金额
1 收 USD 100
1 付 USD 20
1 收 RMB 200
2 付 RUB 100
现在 需要得到下面的表
流水号 USD收 USD付 USD利润(收-付) RMB收 RMB付 。。。
1 100 20 80 200 0
2 。。。。
具体语句我我就不写了,你参考这个
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