这是我的桌子
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”存在,则获取其时间戳,如果不存在,则获取事件“到达”的时间戳。 这是我到目前为止所取得的成就:
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
忽略“到达”行。 有什么建议吗?
谢谢
答案 0 :(得分:1)
如果您正在查看“下一个”或“上一个”事件,请使用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');
答案 1 :(得分:0)
尝试一下:
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