表包含以下内容:
+------+----------------+--------------------------------------+
| ID | Date | Timestamp |
+------+----------------+--------------------------------------+
| 1 | 01-01-2020 | 01-01-2020 10:00:00 |
| 1 | 06-01-2020 | 06-01-2020 23:00:00 |
| 1 | 03-02-2020 | 03-02-2020 10:00:00 |
| 2 | 06-01-2020 | 06-01-2020 12:49:00 |
| 2 | 07-03-2020 | 07-03-2020 10:51:00 |
| 3 | 23-01-1992 | 23-01-1992 09:00:00 |
| 3 | 23-01-1992 | 23-01-1992 10:00:00 |
+------+----------------+--------------------------------------+
我想获取的时间戳的最小日期为'06 -01-2020',答案应为ID#2
SELECT
ID,
MIN(Timestamp)
FROM Table
WHERE Date = '06-01-2020'
GROUP BY ID
我对上述内容的理解将在获得最小时间戳之前首先过滤日期,该最小时间戳会给出ID#1和#2的错误结果。请告知。
答案 0 :(得分:1)
尝试首先在整个表格中找到最小值,然后限制为感兴趣的日期:
SELECT ID, min_timestamp
FROM
(
SELECT ID, MIN(Timestamp) AS min_timestamp
FROM yourTable
GROUP BY ID
) t
WHERE Date = '2020-06-01';
答案 1 :(得分:0)
您似乎想要两个条件:
MIN(timestamp)
ID
在特定日期有记录您可以HAVING
:
SELECT ID, MIN(Timestamp)
FROM Table
GROUP BY ID
HAVING SUM(CASE WHEN Date = '2020-06-01' THEN 1 ELSE 0 END) > 0;
这通过id
进行汇总,并返回最小时间戳。然后,它会过滤ID,因此仅过滤日期为2020-06-01的ID。
如果您'2020-06-01'
是表中的最新日期,则可以将其简化为:
HAVING MAX(Date) = '2020-06-01'
答案 2 :(得分:0)
这将返回具有最小时间戳记的ID(于06-01-2020
)CASE
WHEN E_ID = EMP_ID AND F_NAME = FIRST_NAME AND CITY = LOCATION THEN 'REJECT'
WHEN E_ID IS NULL AND F_NAME IS NULL AND CITY IS NULL THEN 'INSERT'
END FLAG
或没有日期列
SELECT
ID,
MIN(Timestamp)
FROM Table
GROUP BY ID
HAVING min(Date) = date '2020-01-06'