数据集中不同阶段的时间差
Time difference between different stages in data set
我想知道你是否可以帮助我。我不确定这是否可行,但考虑到下面的 table 数据,我想知道是否可以编写一个查询来轻松显示每辆车在 carViewed 和 carBought 阶段之间花费的时间。理想情况下,我希望看到 carID 和时间。例如,结果应该是这样的:
CarID
TimeDifference
1
00:17:83
2
00:04:21
3
01:57:83
数据
CarID
Stage
Timestamp
1
carArrived
2022-01-20 13:00:00
1
carViewed
2022-01-20 14:00:00
1
carBought
2022-01-20 14:17:83
1
carLeft
2022-01-20 15:17:83
2
carArrived
2022-01-21 15:00:00
2
carViewed
2022-01-21 16:00:00
2
carBought
2022-01-21 16:04:21
2
carLeft
2022-01-21 16:27:83
3
carArrived
2022-01-22 13:00:00
3
carViewed
2022-01-22 14:00:00
3
carBought
2022-01-22 15:57:83
3
carLeft
2022-01-22 16:17:83
如有任何帮助,我们将不胜感激。谢谢。
使用条件聚合:
SELECT carid,
MAX(CASE stage WHEN 'carBought' THEN timestamp END)
- MIN(CASE stage WHEN 'carViewed' THEN timestamp END) AS timeDifference
FROM table_name
GROUP BY carid
其中,对于示例数据:
CREATE TABLE table_name (CarID, Stage, Timestamp) AS
SELECT 1, 'carArrived', TIMESTAMP '2022-01-20 13:00:00' FROM DUAL UNION ALL
SELECT 1, 'carViewed', TIMESTAMP '2022-01-20 14:00:00' FROM DUAL UNION ALL
SELECT 1, 'carBought', TIMESTAMP '2022-01-20 14:17:53' FROM DUAL UNION ALL
SELECT 1, 'carLeft', TIMESTAMP '2022-01-20 15:17:53' FROM DUAL UNION ALL
SELECT 2, 'carArrived', TIMESTAMP '2022-01-21 15:00:00' FROM DUAL UNION ALL
SELECT 2, 'carViewed', TIMESTAMP '2022-01-21 16:00:00' FROM DUAL UNION ALL
SELECT 2, 'carBought', TIMESTAMP '2022-01-21 16:04:21' FROM DUAL UNION ALL
SELECT 2, 'carLeft', TIMESTAMP '2022-01-21 16:27:53' FROM DUAL UNION ALL
SELECT 3, 'carArrived', TIMESTAMP '2022-01-22 13:00:00' FROM DUAL UNION ALL
SELECT 3, 'carViewed', TIMESTAMP '2022-01-22 14:00:00' FROM DUAL UNION ALL
SELECT 3, 'carBought', TIMESTAMP '2022-01-22 15:57:53' FROM DUAL UNION ALL
SELECT 3, 'carLeft', TIMESTAMP '2022-01-22 16:17:53' FROM DUAL;
输出:
CARID
TIMEDIFFERENCE
1
+000000000 00:17:53.000000000
2
+000000000 00:04:21.000000000
3
+000000000 01:57:53.000000000
db<>fiddle here
我想知道你是否可以帮助我。我不确定这是否可行,但考虑到下面的 table 数据,我想知道是否可以编写一个查询来轻松显示每辆车在 carViewed 和 carBought 阶段之间花费的时间。理想情况下,我希望看到 carID 和时间。例如,结果应该是这样的:
CarID | TimeDifference |
---|---|
1 | 00:17:83 |
2 | 00:04:21 |
3 | 01:57:83 |
数据
CarID | Stage | Timestamp |
---|---|---|
1 | carArrived | 2022-01-20 13:00:00 |
1 | carViewed | 2022-01-20 14:00:00 |
1 | carBought | 2022-01-20 14:17:83 |
1 | carLeft | 2022-01-20 15:17:83 |
2 | carArrived | 2022-01-21 15:00:00 |
2 | carViewed | 2022-01-21 16:00:00 |
2 | carBought | 2022-01-21 16:04:21 |
2 | carLeft | 2022-01-21 16:27:83 |
3 | carArrived | 2022-01-22 13:00:00 |
3 | carViewed | 2022-01-22 14:00:00 |
3 | carBought | 2022-01-22 15:57:83 |
3 | carLeft | 2022-01-22 16:17:83 |
如有任何帮助,我们将不胜感激。谢谢。
使用条件聚合:
SELECT carid,
MAX(CASE stage WHEN 'carBought' THEN timestamp END)
- MIN(CASE stage WHEN 'carViewed' THEN timestamp END) AS timeDifference
FROM table_name
GROUP BY carid
其中,对于示例数据:
CREATE TABLE table_name (CarID, Stage, Timestamp) AS
SELECT 1, 'carArrived', TIMESTAMP '2022-01-20 13:00:00' FROM DUAL UNION ALL
SELECT 1, 'carViewed', TIMESTAMP '2022-01-20 14:00:00' FROM DUAL UNION ALL
SELECT 1, 'carBought', TIMESTAMP '2022-01-20 14:17:53' FROM DUAL UNION ALL
SELECT 1, 'carLeft', TIMESTAMP '2022-01-20 15:17:53' FROM DUAL UNION ALL
SELECT 2, 'carArrived', TIMESTAMP '2022-01-21 15:00:00' FROM DUAL UNION ALL
SELECT 2, 'carViewed', TIMESTAMP '2022-01-21 16:00:00' FROM DUAL UNION ALL
SELECT 2, 'carBought', TIMESTAMP '2022-01-21 16:04:21' FROM DUAL UNION ALL
SELECT 2, 'carLeft', TIMESTAMP '2022-01-21 16:27:53' FROM DUAL UNION ALL
SELECT 3, 'carArrived', TIMESTAMP '2022-01-22 13:00:00' FROM DUAL UNION ALL
SELECT 3, 'carViewed', TIMESTAMP '2022-01-22 14:00:00' FROM DUAL UNION ALL
SELECT 3, 'carBought', TIMESTAMP '2022-01-22 15:57:53' FROM DUAL UNION ALL
SELECT 3, 'carLeft', TIMESTAMP '2022-01-22 16:17:53' FROM DUAL;
输出:
CARID TIMEDIFFERENCE 1 +000000000 00:17:53.000000000 2 +000000000 00:04:21.000000000 3 +000000000 01:57:53.000000000
db<>fiddle here