求一sql语句,在线等
有一张打卡时间记录表 tableA
表结构如下:
autoid employeeid first second third
1 001 08:30(正常) 11:50(早退) 13:50(迟到)
2 001 08:00(正常) 12:00(正常) 13:45(迟到)
3 002 09:30(迟到) 11:50(早退) 13:50(迟到)
4 002 08:30(正常) 12:00(正常) 13:45(迟到)
我想查询出统计的效果(就是每一个员工对应的迟到、早退、正常总次数):
employeeid normal late early
001 3 2 1
002 2 3 1
高人帮忙
------解决方案--------------------select employeeid ,
SUM(case when patindex( '%正常% ',first)> 0 then 1 else 0 end ) normal,
SUM(case when patindex( '%早退% ',first)> 0 then 1 else 0 end ) late,
SUM(case when patindex( '%迟到% ',first)> 0 then 1 else 0 end ) early
from(
select employeeid , first from tableA union all
select employeeid , second from tableA union all
select employeeid , third from tableA )a
group by employeeid