日期:2014-05-17 浏览次数:20431 次
declare @t table (
username varchar(30),
sj datetime,
pj1 int,
pj2 int,
pj3 int,
pj4 int,
pj5 int
)
insert into @t
select 'user1','2013-1-1 11:11:11',100 ,0,100,0,60 union all
select 'user1','2013-2-1 12:11:11',80,55,50,0,50 union all
select 'user1','2012-1-1 20:11:10',90,77,0,90,70 union all
select 'user2','2012-5-1 05:11:10',70,80,60,0,60
select username,
case when cnt_1>0 then total_1/cnt_1 else 0 end pj1,
case when cnt_2>0 then total_2/cnt_2 else 0 end pj2,
case when cnt_3>0 then total_3/cnt_3 else 0 end pj3,
case when cnt_4>0 then total_4/cnt_4 else 0 end pj4,
case when cnt_5>0 then total_5/cnt_5 else 0 end pj5
from (
select username,
sum(pj1) total_1,sum(case pj1 when 0 then 0 else 1 end) cnt_1,
sum(pj2) total_2,sum(case pj2 when 0 then 0 else 1 end) cnt_2,
sum(pj3) total_3,sum(case pj3 when 0 then 0 else 1 en