如何在 Oracle SQL Developer 中转换 unix 时间戳和聚合最小和最大日期?
How to convert unix timestamp and aggregate min and max date in Oracle SQL Developer?
我在 Oracle SQL 中有 table,如下所示:
ID | date | place
-----------------------------
123 | 1610295784376 | OBJ_1
444 | 1748596758291 | OBJ_1
567 | 8391749204754 | OBJ_2
888 | 1747264526789 | OBJ_3
- ID - 客户端ID
- 日期 - UTC 中的 Unix 时间戳中的日期
- 地点 - 与客户联系的地点
我需要汇总以上日期以获得如下结果,因此我需要:
将 UTC 中的 unix 时间戳从“日期”列转换为正常日期,如下所示
计算“地点”列中每个值的最小和最大日期
min_date
max_date
distinct_place
2022-01-05
2022-02-15
OBJ_1
2022-02-10
2022-03-20
OBJ_2
2021-10-15
2021-11-21
OBJ_3
您可以使用:
SELECT TIMESTAMP '1970-01-01 00:00:00 UTC'
+ MIN(date_column) * INTERVAL '0.001' SECOND(3)
AS min_date,
TIMESTAMP '1970-01-01 00:00:00 UTC'
+ MAX(date_column) * INTERVAL '0.001' SECOND(3)
AS max_date,
place
FROM table_name
GROUP BY place;
注意:SECOND
之后的 (3)
是可选的,只会明确指定小数秒的精度。
或:
SELECT TIMESTAMP '1970-01-01 00:00:00 UTC'
+ NUMTODSINTERVAL( MIN(date_column) / 1000, 'SECOND')
AS min_date,
TIMESTAMP '1970-01-01 00:00:00 UTC'
+ NUMTODSINTERVAL( MAX(date_column) / 1000, 'SECOND')
AS max_date,
place
FROM table_name
GROUP BY place;
其中,对于示例数据:
CREATE TABLE table_name (ID, date_column, place) AS
SELECT 123, 1610295784376, 'OBJ_1' FROM DUAL UNION ALL
SELECT 444, 1748596758291, 'OBJ_1' FROM DUAL UNION ALL
SELECT 567, 1391749204754, 'OBJ_2' FROM DUAL UNION ALL -- Fixed leading digit
SELECT 888, 1747264526789, 'OBJ_3' FROM DUAL;
双输出:
MIN_DATE
MAX_DATE
PLACE
2021-01-10 16:23:04.376000000 UTC
2025-05-30 09:19:18.291000000 UTC
OBJ_1
2014-02-07 05:00:04.754000000 UTC
2014-02-07 05:00:04.754000000 UTC
OBJ_2
2025-05-14 23:15:26.789000000 UTC
2025-05-14 23:15:26.789000000 UTC
OBJ_3
db<>fiddle here
我在 Oracle SQL 中有 table,如下所示:
ID | date | place
-----------------------------
123 | 1610295784376 | OBJ_1
444 | 1748596758291 | OBJ_1
567 | 8391749204754 | OBJ_2
888 | 1747264526789 | OBJ_3
- ID - 客户端ID
- 日期 - UTC 中的 Unix 时间戳中的日期
- 地点 - 与客户联系的地点
我需要汇总以上日期以获得如下结果,因此我需要:
将 UTC 中的 unix 时间戳从“日期”列转换为正常日期,如下所示
计算“地点”列中每个值的最小和最大日期
min_date max_date distinct_place 2022-01-05 2022-02-15 OBJ_1 2022-02-10 2022-03-20 OBJ_2 2021-10-15 2021-11-21 OBJ_3
您可以使用:
SELECT TIMESTAMP '1970-01-01 00:00:00 UTC'
+ MIN(date_column) * INTERVAL '0.001' SECOND(3)
AS min_date,
TIMESTAMP '1970-01-01 00:00:00 UTC'
+ MAX(date_column) * INTERVAL '0.001' SECOND(3)
AS max_date,
place
FROM table_name
GROUP BY place;
注意:SECOND
之后的 (3)
是可选的,只会明确指定小数秒的精度。
或:
SELECT TIMESTAMP '1970-01-01 00:00:00 UTC'
+ NUMTODSINTERVAL( MIN(date_column) / 1000, 'SECOND')
AS min_date,
TIMESTAMP '1970-01-01 00:00:00 UTC'
+ NUMTODSINTERVAL( MAX(date_column) / 1000, 'SECOND')
AS max_date,
place
FROM table_name
GROUP BY place;
其中,对于示例数据:
CREATE TABLE table_name (ID, date_column, place) AS
SELECT 123, 1610295784376, 'OBJ_1' FROM DUAL UNION ALL
SELECT 444, 1748596758291, 'OBJ_1' FROM DUAL UNION ALL
SELECT 567, 1391749204754, 'OBJ_2' FROM DUAL UNION ALL -- Fixed leading digit
SELECT 888, 1747264526789, 'OBJ_3' FROM DUAL;
双输出:
MIN_DATE MAX_DATE PLACE 2021-01-10 16:23:04.376000000 UTC 2025-05-30 09:19:18.291000000 UTC OBJ_1 2014-02-07 05:00:04.754000000 UTC 2014-02-07 05:00:04.754000000 UTC OBJ_2 2025-05-14 23:15:26.789000000 UTC 2025-05-14 23:15:26.789000000 UTC OBJ_3
db<>fiddle here