f_readdate              f_consumerid
----------------------- ------------
2010-09-26 16:15:48.000 37
2010-09-26 16:15:56.000 37
2010-09-26 16:16:54.000 37
2010-09-26 16:18:52.000 37
2010-09-26 16:19:18.000 37
2010-09-26 16:20:10.000 37
2010-09-27 08:45:08.000 37
2010-09-27 17:35:08.000 37
2010-09-28 08:32:06.000 37
2010-09-28 17:42:08.000 37
想要得到如下结果:consumerid      begindate    enddate
----------       -------      --------
37     2010-09-26 16:15:48    16:20:10
37     2010-09-27 08:45:08    17:35:08
37     2010-09-28 08:32:06    17:42:08上面的是公司考勤机上得到的原始数据,想得到每个人每天第一次刷卡和最后一次刷卡的时间,
并以每天每员工为一条记录形式生成结果.百思不得其解,望各位相助!

解决方案 »

  1.   

    --> 测试数据:#
    if object_id('tempdb.dbo.#') is not null drop table #
    create table #(f_readdate datetime, f_consumerid int)
    insert into #
    select '2010-09-26 16:15:48.000', 37 union all
    select '2010-09-26 16:15:56.000', 37 union all
    select '2010-09-26 16:16:54.000', 37 union all
    select '2010-09-26 16:18:52.000', 37 union all
    select '2010-09-26 16:19:18.000', 37 union all
    select '2010-09-26 16:20:10.000', 37 union all
    select '2010-09-27 08:45:08.000', 37 union all
    select '2010-09-27 17:35:08.000', 37 union all
    select '2010-09-28 08:32:06.000', 37 union all
    select '2010-09-28 17:42:08.000', 37select
    ltrim(f_consumerid)+' '+convert(varchar(10),f_readdate,120) consumerid,
    convert(varchar,min(f_readdate),114) begindate,
    convert(varchar,max(f_readdate),114) enddate
    from # group by ltrim(f_consumerid)+' '+convert(varchar(10),f_readdate,120)/*
    consumerid              begindate                      enddate
    ----------------------- ------------------------------ ------------------------------
    37 2010-09-26           16:15:48:000                   16:20:10:000
    37 2010-09-27           08:45:08:000                   17:35:08:000
    37 2010-09-28           08:32:06:000                   17:42:08:000
    */
      

  2.   

    --> 测试数据:#
    if object_id('tempdb.dbo.#') is not null drop table #
    create table #(f_readdate datetime, f_consumerid int)
    insert into #
    select '2010-09-26 16:15:48.000', 37 union all
    select '2010-09-26 16:15:56.000', 37 union all
    select '2010-09-26 16:16:54.000', 37 union all
    select '2010-09-26 16:18:52.000', 37 union all
    select '2010-09-26 16:19:18.000', 37 union all
    select '2010-09-26 16:20:10.000', 37 union all
    select '2010-09-27 08:45:08.000', 37 union all
    select '2010-09-27 17:35:08.000', 37 union all
    select '2010-09-28 08:32:06.000', 37 union all
    select '2010-09-28 17:42:08.000', 37select
    f_consumerid consumerid,
    min(f_readdate) begindate,
    convert(varchar,max(f_readdate),114) enddate
    from # group by f_consumerid, convert(varchar,f_readdate,112)/*
    consumerid  begindate               enddate
    ----------- ----------------------- ------------------------------
    37          2010-09-26 16:15:48.000 16:20:10:000
    37          2010-09-27 08:45:08.000 17:35:08:000
    37          2010-09-28 08:32:06.000 17:42:08:000
    */
      

  3.   

    恩 我的做法是,只说个步骤思路,你可以自己去实践一下,实践出真知
    1 生成临时表,插入每天的日期数据(并且去除不需要考勤的日期)
    2 用cross匹配每一个员工信息,然后left join做匹配刷卡记录(字符串可能需要处理一下),得出每天最大时间和最小时间
    所以你一共需要准备3个表
    1 员工信息表
    2 考勤记录表
    3 临时表记录每天的信息(去除休息天)
      

  4.   

    select
        a.f_consumerid UserID,
    b.f_consumerName NickName,
        convert(varchar,f_readdate,112) dateday,
    convert(varchar,min(f_readdate),114) datebegin,
        convert(varchar,max(f_readdate),114) dateend
    FROM t_d_CardRecord a join t_b_Consumer b on a.f_consumerid=b.f_ConsumerID
    --WHERE (a.f_ConsumerID = 37)
     group by a.f_consumerid, convert(varchar,f_readdate,112),b.f_consumername
    哈哈,成了.