簡體   English   中英

使用MS Access或SQL Server的出勤日志

[英]Attendance Log using MS Access or SQL Server

我已經工作了將近一個月,但我想我現在需要一些幫助。 我在下面有時間日志。 我正在使用MS Access和C#。 請幫忙選擇查詢

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

我想成為這個。請幫忙。 什么是獲取此數據的最佳查詢。

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

下面的查詢應該接近您想要的:

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

我對您的徽章1708009預期輸出有疑問,因為該徽章的最早檢查時間是7:11 ,而不是7:12

我會在subquery使用row_number()

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM