根据列值获取值
Grab value based on column value
这是我的 table:
idMission Event Timestamp
1 detectedBubbleOut 2018-05-26T12:53:43.000Z
1 arrived 2018-05-26T13:58:57.000Z
1 detectedInBuilding 2018-05-26T12:42:20.000Z
1 detectedBubbleIn 2018-05-26T12:37:36.000Z
1 detectedTimeOut 2018-05-26T12:52:17.000Z
1 scanFulfilled 2018-05-26T12:41:26.000Z
1 detectedBubbleIn 2018-05-26T12:37:22.000Z
1 customerInteraction 2018-05-26T13:59:04.000Z
1 scanFulfilled 2018-05-26T13:59:01.000Z
1 delFulfilled 2018-05-26T12:48:30.000Z
1 eventFulfilled 2018-05-26T12:48:30.000Z
1 nextDelivery 2018-05-26T12:50:20.000Z
1 customerInteraction 2018-05-26T12:48:18.000Z
1 detectedOutBuilding 2018-05-26T12:49:21.000Z
1 arrived 2018-05-26T12:40:09.000Z
1 detectedTimeIn 2018-05-26T12:38:58.000Z
2 nextDelivery 2018-05-27T12:50:20.000Z
2 customerInteraction 2018-05-27T12:48:18.000Z
2 detectedOutBuilding 2018-05-27T12:49:21.000Z
2 arrived 2018-05-27T12:40:09.000Z
2 detectedTimeIn 2018-05-27T12:38:58.000Z
有些事件与时间戳相关联,这是事件发生的时间。我专注于事件 "arrived" 和 "detectedTimeIn",但事件 "detectedTimeIn" 并不总是可用,所以我使用 "arrived"。
我想要的只是根据特定事件获取时间戳。
如果事件 "detectedTimeIn" 存在,那么我将获取它的时间戳,如果它不存在,那么我将获取事件的时间戳 "arrived"。
这是我到目前为止所取得的成就:
select
event,
stp."timestamp" as TimeIN
from main_source_execevent_coop stp
where event Coalesce('detectedTimeIn', 'arrived')
预期结果:
1 detectedTimeIn 2018-05-26T12:38:58.000Z
2 arrived 2018-05-27T12:40:09.000Z
但它不起作用我只获得:
1 检测时间在 2018-05-26T12:38:58.000Z
忽略 "arrived" 行。
你有什么建议吗?
谢谢
如果您正在查看 "next" 或 "previous" 事件,请使用 lead()
/lag()
:
select t.*
from (select stp.*,
lag(event) over (order by timestamp) as prev_event
from main_source_execevent_coop stp
) stp
where event = 'detectedTimeIn' or
(event = 'arrived' and next_event is distinct from 'detectedTimeIn');
试试这个:
SELECT * FROM
(SELECT event,
stp."timestamp" as TimeIN
FROM your_table
WHERE event ='detectedTimeIn'
LIMIT 1
UNION ALL
SELECT event,
stp."timestamp" as TimeIN
FROM your_table
WHERE event ='arrived'
LIMIT 1) a
LIMIT 1
这是我的 table:
idMission Event Timestamp
1 detectedBubbleOut 2018-05-26T12:53:43.000Z
1 arrived 2018-05-26T13:58:57.000Z
1 detectedInBuilding 2018-05-26T12:42:20.000Z
1 detectedBubbleIn 2018-05-26T12:37:36.000Z
1 detectedTimeOut 2018-05-26T12:52:17.000Z
1 scanFulfilled 2018-05-26T12:41:26.000Z
1 detectedBubbleIn 2018-05-26T12:37:22.000Z
1 customerInteraction 2018-05-26T13:59:04.000Z
1 scanFulfilled 2018-05-26T13:59:01.000Z
1 delFulfilled 2018-05-26T12:48:30.000Z
1 eventFulfilled 2018-05-26T12:48:30.000Z
1 nextDelivery 2018-05-26T12:50:20.000Z
1 customerInteraction 2018-05-26T12:48:18.000Z
1 detectedOutBuilding 2018-05-26T12:49:21.000Z
1 arrived 2018-05-26T12:40:09.000Z
1 detectedTimeIn 2018-05-26T12:38:58.000Z
2 nextDelivery 2018-05-27T12:50:20.000Z
2 customerInteraction 2018-05-27T12:48:18.000Z
2 detectedOutBuilding 2018-05-27T12:49:21.000Z
2 arrived 2018-05-27T12:40:09.000Z
2 detectedTimeIn 2018-05-27T12:38:58.000Z
有些事件与时间戳相关联,这是事件发生的时间。我专注于事件 "arrived" 和 "detectedTimeIn",但事件 "detectedTimeIn" 并不总是可用,所以我使用 "arrived"。 我想要的只是根据特定事件获取时间戳。 如果事件 "detectedTimeIn" 存在,那么我将获取它的时间戳,如果它不存在,那么我将获取事件的时间戳 "arrived"。 这是我到目前为止所取得的成就:
select
event,
stp."timestamp" as TimeIN
from main_source_execevent_coop stp
where event Coalesce('detectedTimeIn', 'arrived')
预期结果:
1 detectedTimeIn 2018-05-26T12:38:58.000Z
2 arrived 2018-05-27T12:40:09.000Z
但它不起作用我只获得: 1 检测时间在 2018-05-26T12:38:58.000Z
忽略 "arrived" 行。 你有什么建议吗?
谢谢
如果您正在查看 "next" 或 "previous" 事件,请使用 lead()
/lag()
:
select t.*
from (select stp.*,
lag(event) over (order by timestamp) as prev_event
from main_source_execevent_coop stp
) stp
where event = 'detectedTimeIn' or
(event = 'arrived' and next_event is distinct from 'detectedTimeIn');
试试这个:
SELECT * FROM
(SELECT event,
stp."timestamp" as TimeIN
FROM your_table
WHERE event ='detectedTimeIn'
LIMIT 1
UNION ALL
SELECT event,
stp."timestamp" as TimeIN
FROM your_table
WHERE event ='arrived'
LIMIT 1) a
LIMIT 1