日期:2014-05-17 浏览次数:20566 次
SELECT AgtentId,[type 1]=SUM(CASE WHEN type = 1 THEN money ELSE 0 END),
[type 2]=SUM(CASE WHEN type = 2 THEN money ELSE 0 END) ,
date
FROM dbo.TB
GROUP BY AgtentId,date
ORDER BY date
/*
AgtentId type 1 type 2 date
1 20.00 0.00 2012-12-20 00:00:00.000
1 10.00 0.00 2012-12-27 00:00:00.000
2 0.00 10.00 2012-12-27 00:00:00.000*/
with tb(a,b,c,d,e) as(
select 1,1,10.00,1,'2012-12-27' union all
select 2,2,10.00,2,'2012-12-27' union all
select 3,1,10.00,1,'2012-12-20' union all
select 4,1,10.00,1,'2012-12-20')
select b,SUM(case when b=1 then c else 0 end),SUM(case when b=2 then c else 0 end),
e from tb group by b,e order by e desc