我有一张包含以下信息的表格:
数据样本
**Table 1**
palletNumber-- event-- --recordDate
-----1-----------A-------01/11/2015 01:00
-----1-----------B-------01/11/2015 02:00
-----1-----------C-------01/11/2015 03:00
-----1-----------D-------01/11/2015 04:00
-----2-----------A-------01/11/2015 01:10
-----2-----------C-------01/11/2015 01:15
-----2-----------E-------01/11/2015 01:20
我想通过palletNumber选择recordDate序列中表中出现的所有可能事件组合。我尝试使用行号,超过分区的各种语句,但这并没有让我接近我正在寻找的东西......任何方向去哪儿?
这将是输出表,例如:
**Table 2**
event1-- event2--
---A------B------
---B------C------
---C------D------
---A------C------
---C------E------
谢谢,
答案 0 :(得分:2)
您可以使用lag()
或lead()
select event,
lead(event) over (partition by palletnumber order by recorddate) as next_event
from datasample;
如果你想消除重复,我倾向于使用group by
,因为这也可以计算每对出现的次数:
select event, next_event, count(*) as cnt
from (select event,
lead(event) over (partition by palletnumber order by recorddate) as next_event
from datasample
) ds
group by event, next_event;
答案 1 :(得分:0)
用例:
select case when palletNumber = 1 then event else null end as event1,
case when palletNumber = 2 then event else null end as event2,
recordDate
from table1
然后,您可以使用潜在客户 / 滞后或总和()/ 分组来处理数据把它放在一排。
假设事件1/2每日只有一条记录
select recordDate, max (event1), max (event2)
from ( select case when palletNumber = 1 then event else null end as event1,
case when palletNumber = 2 then event else null end as event2,
recordDate
from table1
order by recordDate) tab2
group by recordDate