SQL Oracle - 根据日期查找所有可能的事件组合
SQL Oracle - Find all combination of events possible based on date
我有一个 table,其中包含以下信息:
数据样本
**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
我想select所有可能的事件组合出现在table中的recordDate by palletNumber的序列中。我尝试了行号、分区之上的各种语句,但这并没有让我接近我正在寻找的东西......关于去哪里的任何方向?
这将是输出 table 例如:
**Table 2**
event1-- event2--
---A------B------
---B------C------
---C------D------
---A------C------
---C------E------
谢谢,
您可以使用 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;
用例:
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
然后您可以使用 lead/lag 或 sum() 处理数据/ group by 得到一行。
假设事件 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
我有一个 table,其中包含以下信息:
数据样本
**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
我想select所有可能的事件组合出现在table中的recordDate by palletNumber的序列中。我尝试了行号、分区之上的各种语句,但这并没有让我接近我正在寻找的东西......关于去哪里的任何方向?
这将是输出 table 例如:
**Table 2**
event1-- event2--
---A------B------
---B------C------
---C------D------
---A------C------
---C------E------
谢谢,
您可以使用 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;
用例:
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
然后您可以使用 lead/lag 或 sum() 处理数据/ group by 得到一行。
假设事件 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