简体   繁体   中英

Attendance Log using MS Access or SQL Server

I'm working this almost a month but I think I need some help now. I have a time logs below. I'm using MS Access and C#. Please help what select query

ID  BADGE   CHECKTIME
-----------------------------
1   1507010 5/31/2018 8:51
1   1507010 5/31/2018 19:52
2   1708004 5/31/2018 6:35
2   1708004 5/31/2018 13:43
3   1708005 5/31/2018 19:23
3   1708005 6/1/2018 8:34
4   1708006 5/31/2018 7:51
4   1708006 6/1/2018 18:34
5   1708007 5/31/2018 19:23
5   1708007 6/1/2018 6:36
6   1708009 5/31/2018 7:11
6   1708009 5/31/2018 7:12
6   1708009 5/31/2018 22:02
6   1708009 5/31/2018 22:03

I want to become this.please help. what the best query to get this data.

ID  Badge   IN              OUT
--------------------------------------------
1   1507010 5/31/2018 8:51  5/31/2018 13:43
2   1708004 5/31/2018 6:35  5/31/2018 13:43
3   1708005 5/31/2018 19:23 6/1/2018 8:34
4   1708006 5/31/2018 7:51  6/1/2018 18:34
5   1708007 5/31/2018 19:23 6/1/2018 6:36
6   1708009 5/31/2018 7:12  5/31/2018 22:03

The following query should get close to what you want:

SELECT
    ID,
    Badge,
    MIN(CHECKTIME) AS [IN],
    MAX(CHECKTIME) AS [OUT]
FROM yourTable
GROUP BY
    ID,
    Badge;

I have a doubt about your expected output for badge 1708009 , since the earliest check time for that badge is 7:11 , not 7:12 .

I would use row_number() inside the subquery :

select id, badge, min(checktime) as in, max(checktime) as out
from (select *,  row_number() over (partition by id, badge, cast(checktime as date), datepart(hh,checktime) 
                                       order by datepart(mm,checktime) desc) seq
      from table 
     ) t
where seq = 1
group by id, badge;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM