日期:2014-05-18  浏览次数:20833 次

请各位兄弟给看看。 如何实现这个查询.
userid 加班时长 调休时长 请假时长 迟到时间 早退时间 矿工时间 确认 剩余调休时间 日期
1 1 9:10 是 15 2007-1-1
1 2 是 15 2007-1-2
1 2 9:10 是 15 2007-1-3
1 2 11:30 是 15 2007-1-4
2 2 9:10 是 40 2007-1-1
2 2 9:10 是 40 2007-1-2
2 3 4:10 是 40 2007-1-3
2 2 9:10 是 40 2007-1-4
2 2 9:10 是 40 2007-1-5
3 1 9:10 是 35 2007-1-1
3 1 9:10 是 35 2007-1-2
3 1 9:10 是 35 2007-1-3
3 5 9:10 是 35 2007-1-4
3 1 9:10 是 35 2007-1-5




2007-1-1 至2007-1-3

userid 加班时长(小时) 调休时长(小时) 请假时长(小时) 迟到次数(次) 早退次数(次) 旷工次数(次) 剩余调休时间(小时)
1 3 2 0 2 0 0 15
2 0 4 3 2 1 0 40
3 3 0 0 0 3 0 35



请各位兄弟给看看。 如何实现这个查询. 迟到.早退.旷工算次数, 加班时长,调休时长,请假时长 酸小时 剩余调休时间 也算小时  


这个查询把我弄晕了

------解决方案--------------------
select userid,sum(加班时长) as 加班时长,sum(调休时长) as 调休时长,sum(请假时长) as 请假时长,
sum(case when 迟到时间 is null then 0 else 1 end) as 迟到时间 ,
sum(case when 早退时间 is null then 0 else 1 end) as 早退时间,
sum(case when 旷工次数 is null then 0 else 1 end) as 旷工次数,
sum(剩余调休时间) as 剩余调休时间
from table 
where 日期>='2007-1-1' and 日期<='2007-1-3' 
group by userid
order by userid
------解决方案--------------------
SQL code
select 
userid,
sum(加班时长) 加班时长,sum(调休时长) 调休时长,sum(请假时长)请假时长,
sum(case when 迟到时间<>'' then 1 else 0 end)迟到次数,
sum(case when 早退时间<>'' then 1 else 0 end )早退次数,
sum(case when 矿工时间<>'' then 1 else 0 end ) 旷工次数,
min(剩余调休时间) 剩余调休时间
from @a 
where 日期 between '2007-1-1' and '2007-1-3' 
group by userid