我正在尝试获得按用户组织的最小时间戳,然后是特定操作的日期。谢谢你的帮助。
我研究了下面的代码,但不确定。
SELECT a1.*
FROM accesscards a1
JOIN (SELECT name, MIN(entrydates) mindate
FROM accesscards
WHERE name != ''
GROUP BY name, date(entrydates)) a2
ON a1.name = a2.name AND a1.entrydates = a2.mindate
来自Get records for each person's each day's min datetime
当前表
+----------+------+--------+---------------------+
| idRecord | user | action | recorded |
+----------+------+--------+---------------------+
| 1 | bob | start | 2018-01-27 01:00:01 |
+----------+------+--------+---------------------+
| 2 | amy | close | 2018-01-27 01:00:01 |
+----------+------+--------+---------------------+
| 3 | bob | start | 2018-01-27 02:00:01 |
+----------+------+--------+---------------------+
| 4 | amy | start | 2018-01-27 00:00:01 |
+----------+------+--------+---------------------+
| 5 | amy | start | 2018-01-28 00:00:01 |
+----------+------+--------+---------------------+
| 6 | jim | open | 2018-01-28 00:00:01 |
+----------+------+--------+---------------------+
| 7 | bob | close | 2018-01-27 02:00:01 |
+----------+------+--------+---------------------+
| 8 | bob | start | 2018-01-28 00:00:01 |
+----------+------+--------+---------------------+
| 9 | jim | close | 2018-01-28 00:00:01 |
+----------+------+--------+---------------------+
期望的结果
+------+----------+--------+---------------------+
| User | Date | action | recorded |
+------+----------+--------+---------------------+
| amy | 01-27-18 | start | 2018-01-27 00:00:01 |
+------+----------+--------+---------------------+
| amy | 01-28-18 | start | 2018-01-28 00:00:01 |
+------+----------+--------+---------------------+
| bob | 01-27-18 | start | 2018-01-28 01:00:01 |
+------+----------+--------+---------------------+
| bob | 01-28-18 | start | 2018-01-28 00:00:01 |
+------+----------+--------+---------------------+
| jim | 01-28-18 | start | 2018-01-28 00:00:01 |
+------+----------+--------+---------------------+
答案 0 :(得分:1)
您需要使用name
,action
和date(entrydates)
SELECT name, date(entrydates), action, MIN(entrydates)
FROM accesscards
WHERE action='start'
group by name, action, date(entrydates)
使用WHERE action='start'
确保我们不会对多个实例进行分组('开始'以及'关闭'),action
类型start
总是最小的。