如何为用户检索每日打卡时间?

时间:2017-11-07 10:20:18

标签: mysql

我有一个Punchtime表,其中包含User Punchdata。

User    |   Punchtime   |   DatePunch   |   createdon
user1   |   08:57:26    |   2017-06-01  |   2017-06-02 15:09:46
user1   |   08:57:27    |   2017-06-01  |   2017-06-02 15:09:46
user2   |   14:11:29    |   2017-06-01  |   2017-06-02 15:09:46
user2   |   19:26:58    |   2017-06-01  |   2017-06-02 15:09:46
user1   |   18:27:00    |   2017-06-01  |   2017-06-02 15:09:47

我希望DatePunch获取最小和最大用户打卡时间,其差异为三小时。 如果差异小于三小时,则Outtime将返回null

所需的输出就像。

User    |   InTime      |   Outime      |   Date
user1   |   08:57:26    |   18:27:00    |   2017-06-01
user2   |   14:11:29    |   19:26:58    |   2017-06-01

2 个答案:

答案 0 :(得分:1)

我的查询有一个解决方案,但它似乎太慢了。如果有人提供了更多的建议。

 SELECT User,CAST(MIN(Punchtime) AS TIME) AS InTime,
            (CASE
                WHEN
                    ((TIME_TO_SEC(TIMEDIFF(MAX(Punchtime),
                                    MIN(Punchtime))) / 3600) > 3)
                THEN
                    CAST(MAX(Punchtime) AS TIME)
                ELSE NULL
            END) AS OutTime
    FROM DailyPunchInOut GROUP BY Date,User

答案 1 :(得分:0)

尝试:

Your system information:
Cordova CLI: 7.1.0 
Ionic CLI Version: 2.2.1
Ionic App Lib Version: 2.2.0
ios-deploy version: 1.9.0 
ios-sim version: 5.0.13 
OS: macOS Sierra
Node Version: v9.0.0
Xcode version: Xcode 9.1 Build version 9B55