求计算加班加点次数的SQL语句

求计算加班次数的SQL语句
表一:
 员工姓名 员工工号 刷卡时间
 蔡燕 20513 2014-6-27 18:20:45
蔡燕 20513 2014-6-27 18:20:47
蔡燕 20513 2014-6-30 8:14:54
蔡燕 20513 2014-7-1 8:17:51
蔡燕 20513 2014-7-1 18:10:50
蔡燕 20513 2014-7-2 8:16:55
蔡燕 20513 2014-7-2 20:09:44
蔡燕 20513 2014-7-3 8:25:18
蔡燕 20513 2014-7-3 18:04:02
蔡燕 20513 2014-7-3 18:04:48
蔡燕 20513 2014-7-3 18:04:50
艾云东 11304 2014-06-23 08:04:18
艾云东 11304 2014-06-23 16:40:50
艾云东 11304 2014-06-24 08:06:47
艾云东 11304 2014-06-24 10:04:23
艾云东 11304 2014-06-24 20:40:39
艾云东 11304 2014-06-25 08:03:31
艾云东 11304 2014-06-25 20:40:19
超过9个小时算加班,我想根据表一计算出某个员工某天是否加班和一个月内的加班次数,得出类似下表:
 姓名          次数
艾云东        2
蔡燕         3
……

 注意:有些是只打上班卡或者只打下班卡不算加班。一天中连续打了多次卡以一天中最早一次卡为上班卡,最晚一次为下班卡 
------解决思路----------------------

with a1 (员工姓名,员工工号,刷卡时间) as
(
select '蔡燕',20513,'2014-06-27 18:20:45' union all
select '蔡燕',20513,'2014-06-27 18:20:47' union all
select '蔡燕',20513,'2014-06-30 08:14:54' union all
select '蔡燕',20513,'2014-07-01 08:17:51' union all
select '蔡燕',20513,'2014-07-01 18:10:50' union all
select '蔡燕',20513,'2014-07-02 08:16:55' union all
select '蔡燕',20513,'2014-07-02 20:09:44' union all
select '蔡燕',20513,'2014-07-03 08:25:18' union all
select '蔡燕',20513,'2014-07-03 18:04:02' union all
select '蔡燕',20513,'2014-07-03 18:04:48' union all
select '蔡燕',20513,'2014-07-03 18:04:50' union all
select '艾云东',11304,'2014-06-23 08:04:18' union all
select '艾云东',11304,'2014-06-23 16:40:50' union all
select '艾云东',11304,'2014-06-24 08:06:47' union all
select '艾云东',11304,'2014-06-24 10:04:23' union all
select '艾云东',11304,'2014-06-24 20:40:39' union all
select '艾云东',11304,'2014-06-25 08:03:31' union all
select '艾云东',11304,'2014-06-25 20:40:19'
)
,a2 as
(
select 员工姓名,convert(char(10),刷卡时间,120) 日期,min(刷卡时间) 上班时间,max(刷卡时间) 下班时间
,datediff(second,min(刷卡时间),max(刷卡时间)) 上班时长
from a1
group by 员工姓名,convert(char(10),刷卡时间,120)
having datediff(second,min(刷卡时间),max(刷卡时间))>32400
)
select 员工姓名,count(*) as 次数
from a2
group by 员工姓名

------解决思路----------------------
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

Quote: 引用:

Quote: 引用:

Quote: 引用:

select 员工工号,员工姓名,count(员工工号) as counts  from (select 员工工号,员工姓名 ,max(刷卡日期) as endtime from tablename 
group by 员工工号,员工姓名,convert(char(10),刷卡时间,120) 日期) as t
where t.endtime>'2014-06-25 20:00:00'
group by 员工工号,员工姓名,convert(char(7),刷卡时间,120) 

我要的是这种效果
计算出加班次数 姓名          次数
 艾云东        2
蔡燕         3
……
t.endtime>'2014-06-25 20:00:00'有日期 里面了,是要求含有20:00:00以后打卡的就算加班



with a1 (员工姓名,员工工号,刷卡时间) as
(
select '蔡燕',20513,'2014-06-27 18:20:45' union all
select '蔡燕',20513,'2014-06-27 18:20:47' union all
select '蔡燕',20513,'2014-06-30 08:14:54' union all
select '蔡燕',20513,'2014-07-01 08:17:51' union all
select '蔡燕',20513,'2014-07-01 18:10:50' union all
select '蔡燕',20513,'2014-07-02 08:16:55' union all
select '蔡燕',20513,'2014-07-02 20:09:44' union all
select '蔡燕',20513,'2014-07-03 08:25:18' union all
select '蔡燕',20513,'2014-07-03 18:04:02' union all
select '蔡燕',20513,'2014-07-03 18:04:48' union all
select '蔡燕',20513,'2014-07-03 18:04:50' union all
select '艾云东',11304,'2014-06-23 08:04:18' union all
select '艾云东',11304,'2014-06-23 16:40:50' union all
select '艾云东',11304,'2014-06-24 08:06:47' union all
select '艾云东',11304,'2014-06-24 10:04:23' union all
select '艾云东',11304,'2014-06-24 20:40:39' union all
select '艾云东',11304,'2014-06-25 08:03:31' union all
select '艾云东',11304,'2014-06-25 20:40:19'
)

select 员工工号,员工姓名,count(员工工号) as counts  from (select 员工工号,员工姓名 ,max(刷卡时间) as 刷卡时间 from a1
group by 员工工号,员工姓名,convert(char(10),刷卡时间,120)) as t