--> ²âÊÔÊý¾Ý: @a declare @a table (a_date datetime,qtime varchar(5), a_log varchar(1),a_rs int,a_ys int) insert into @a select '2009-11-1','07:00','T',1,7 union all select '2009-11-1','07:00','T',2,8 union all select '2009-11-1','07:00','T',2,8 union all select '2009-11-1','08:00','T',3,9 union all select '2009-11-1','08:00','T',4,10 union all select '2009-11-1','09:00','F',4,10
select a_date,sum(b.a_rs) as a_rs,sum(b.a_ys) as a_ys, bc1=sum(case when a_log='T' then 1 else 0 end ), bc2=sum(case when a_log='F' then 1 else 0 end ) from ( select a_date,qtime,a_log,sum(a_rs) as a_rs ,sum(a_ys) as a_ys from @a group by a_date,qtime,a_log)b group by a_date,a_log
ÄÜ·ñ½«ÒÔÉÏÓï¾äÓÅ»¯Ò»Ï£¿»òÕßÖ»ÓÃÒ»¸öSELECT£¿SQL code: select a_date, sum(case when a_log='T' then a_rs else 0 end) as a_rs, sum(case when a_log='F' then a_ys else 0 end) as a_ys, bc1=sum(case when a_log='T' then 1 else 0 end ), bc2=sum(case when a_log='F' then 1 else 0 end ) from @a group by a_date