-1

我试图获得每个用户组织的最小时间戳,然后是特定操作的日期。感谢您的任何帮助。

我研究了下面的代码,但不确定。

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

获取每个人每天的最小日期时间的记录

当前表

+----------+------+--------+---------------------+
| 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 |
+------+----------+--------+---------------------+
4

1 回答 1

1

您需要按name,actiondate(entrydates)

SELECT name, date(entrydates), action, MIN(entrydates)
FROM accesscards
WHERE action='start'
group by name, action, date(entrydates)

使用WHERE action='start'确保我们不分组多个实例(“开始”和“关闭”),action类型start总是最小的。

于 2018-01-27T16:43:55.083 回答