简体   繁体   English

每天获取第一条记录,然后仅检查条件

[英]get first record from each day then only check condition

I have user attendance CLOCK IN data like this. 我有这样的用户出席率CLOCK IN数据。

id  | userID | created_at
1   | 1      | 2018-06-27 00:15:00
2   | 1      | 2018-06-27 01:43:55
3   | 1      | 2018-06-27 02:43:55
4   | 2      | 2018-06-27 00:15:00
5   | 2      | 2018-06-27 02:43:55
6   | 2      | 2018-06-27 03:43:55
7   | 1      | 2018-06-28 00:55:00
8   | 1      | 2018-06-28 01:43:55
9   | 1      | 2018-06-28 02:43:55
10  | 2      | 2018-06-28 00:00:00
11  | 2      | 2018-06-28 02:43:55
12  | 2      | 2018-06-28 03:43:55

I want a list of dates where user was late to clock in. Assume company work time is 00:00:00 and How can I get results like this : 我想要一个用户迟到的日期列表。假设公司的工作时间是00:00:00,如何获得这样的结果:

id  | userID | created_at
1   | 1      | 2018-06-27 00:15:00
4   | 2      | 2018-06-27 00:15:00
7   | 1      | 2018-06-28 00:55:00

Appreciate any help from you guys.Thanks 🙏🏻 感谢你们的帮助。谢谢han

You could try conditionally aggregating by user and date, and then checking to see whether an exact midnight clock in occurred (or did not occur): 您可以尝试按用户和日期有条件地进行汇总,然后检查是否发生了(或未发生)确切的午夜时钟:

SELECT
    userID,
    MIN(created_at) AS created_at
FROM yourTable
GROUP BY
    userID,
    DATE(created_at)
HAVING
    SUM(CASE WHEN DATE_FORMAT(created_at, '%H:%i:%s') = '00:00:00' THEN 1 ELSE 0 END) = 0;

在此处输入图片说明

Demo 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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