我有一些类似这样的记录
barcode:123, event:ASSIGN, event_datetime 9/23/2018 10am
barcode:123, event:CLOSE, event_datetime 9/23/2018 11am
barcode:123, event:ASSIGN, event_datetime 9/30/2018 10am
barcode:123, event:CLOSE, event_datetime 9/30/2018 11am
我想将这4条记录合并为2条看起来像这样的记录:
barcode:123, event:close, event_start_datetime 9/23/18 10am event_end_datetime 9/23/2018 11am
barcode:123, event:close, event_start_datetime 9/30/2018 10am event_end_datetime 9/30/2018 11am
我尝试了显而易见的MIN/MAX
组合,但是由于所有其他列都是相同的数据(基本上,它们看起来像是重复的,除了一个列),因此它将使用最小/最大组合合并为一列。
我正在考虑使用rank()
进行操作,但这听起来似乎变得非常复杂,我可以寻求帮助。
答案 0 :(得分:0)
不是最优雅的答案,请尝试以下
select barcode, last_event_act, max(max_event_start), max(max_event_end) , event_datetime from
(
select barcode, last_event_act,
max(event_end) over (partition by to_char(event_end, 'mm/dd/YYYY')) max_event_end,
max(event_start) over (partition by to_char(event_start, 'mm/dd/YYYY')) max_event_start, event_datetime
from
(
select barcode, last_value(EVENT) OVER (PARTITION BY to_char(EVENT_DATETIME, 'mm/dd/YYYY') ) as last_event_act,
case
when event = 'CLOSE' then event_datetime
END
as event_end,
case
when event = 'ASSIGN' then event_datetime
END
as event_start , max(to_char(event_datetime, 'mm/dd/yyyy')) over (partition by to_char(event_datetime, 'mm/dd/YYYY')) as event_datetime from barc
)
)
group by barcode, last_event_act, event_datetime