对不住各位,刚才的揭帖了,不知道什么原因没有黏贴好,只黏贴了一部分,下面是完整的问题,我会加分的:表1:打卡流水号 
empid ,int,工号 
recdate,datetime,打卡时间--比如2009/1/22 11:33 
recordid,int,打卡序号 求员工号,每天最早打开时间,每天最晚打卡时间, 
empid ,minrecdate,maxrecdate
特殊例子:
上面每天最早打开时间,每天最晚打卡时间也就是上班时间,和下班时间1:一天只打卡一次,比如:2009/1/8 9:06、2009/1/8 19:06
这俩个值都是某一天的最大,最小纪录
要求,临界值12:00=《12:00  纪录为上班时间,下班时间为空
〉12:00  下班时间,则小半时间为空
2:某日没有打卡:上下班均为空值,也就是说,时间一定要是连续的

解决方案 »

  1.   

    先生成一个连续的ID或连续的日期中间表与下面的结果集关联select empid,recdate,recordid ,case when datepart(hh,recdate) < 12 then 0 else 1 end as type
    from ta
      

  2.   

    declare @t table(empid int ,recdate datetime,recordid int)
    set nocount on
    insert @t select 1,'2001-01-01 09:12:00',1
    insert @t select 1,'2001-01-01 09:13:00',2
    insert @t select 1,'2001-01-01 10:12:00',3
    insert @t select 1,'2001-01-01 16:12:00',4
    insert @t select 1,'2001-01-02 13:12:00',5
    insert @t select 1,'2001-01-01 17:12:00',6
    insert @t select 2,'2001-01-01 09:12:00',1
    insert @t select 2,'2001-01-01 18:12:00',2
    insert @t select 3,'2001-01-01 09:12:00',1select top 30 N=identity(int,0,1) into # from sysobjects a,sysobjects b
    select b.empid,b.d,
    max(minrecdate) ,max(maxrecdate)
    from(
    select empid,
        convert(char(10),recdate,120) as d,
        min( case when datepart(hh,recdate) < 12 then right(convert(char(19),recdate,120),8) else '00:00:00' end ) as minrecdate,
        max( case when datepart(hh,recdate) >= 12 then right(convert(char(19),recdate,120),8) else '00:00:00' end) as maxrecdate
    from @t
    group by empid,convert(char(10),recdate,120),case when datepart(hh,recdate) < 12 then 0 else 1 end)a
    right join 
    ( select 
    empid,
    cast('2001-01-01' as datetime)+N as d 
    from # 
    full join 
    (select distinct empid from @t) c 
    on 1= 1 
    ) b
    on a.d = b.d and a.empid = b.empid
    group by b.empid,b.d
    order by 1,2
    drop table #
    set nocount off/*empid       d                                                                                        
    ----------- ------------------------------------------------------ ---------------- ---------------- 
    1           2001-01-01 00:00:00.000                                09:12:00         17:12:00
    1           2001-01-02 00:00:00.000                                00:00:00         13:12:00
    1           2001-01-03 00:00:00.000                                NULL             NULL
    .......
    1           2001-01-30 00:00:00.000                                NULL             NULL
    2           2001-01-01 00:00:00.000                                09:12:00         18:12:00
    2           2001-01-02 00:00:00.000                                NULL             NULL
    .......
    2           2001-01-30 00:00:00.000                                NULL             NULL
    3           2001-01-01 00:00:00.000                                09:12:00         00:00:00
    3           2001-01-02 00:00:00.000                                NULL             NULL
    3           2001-01-03 00:00:00.000                                NULL             NULL
    .........
    3           2001-01-27 00:00:00.000                                NULL             NULL
    3           2001-01-28 00:00:00.000                                NULL             NULL
    3           2001-01-29 00:00:00.000                                NULL             NULL
    3           2001-01-30 00:00:00.000                                NULL             NULL警告: 聚合或其它 SET 操作消除了空值。*/