Sql Oracle连接之间的连接,间隔时间为分钟
我在按分钟连接具有不同日期间隔的表时遇到一些问题 例子: 表1Sql Oracle连接之间的连接,间隔时间为分钟,sql,oracle,time,setinterval,Sql,Oracle,Time,Setinterval,我在按分钟连接具有不同日期间隔的表时遇到一些问题 例子: 表1 ID Modules Timestamp 1 Delivered 02-FEB-2020 08:24:45 1 Read 02-FEB-2020 08:27:50 1 Delivered 03-FEB-2020 09:24:45 1 Read 03-FEB-2020 10:00:50 2 Delivered 03-FEB-2020 09:28:10 2 Read
ID Modules Timestamp
1 Delivered 02-FEB-2020 08:24:45
1 Read 02-FEB-2020 08:27:50
1 Delivered 03-FEB-2020 09:24:45
1 Read 03-FEB-2020 10:00:50
2 Delivered 03-FEB-2020 09:28:10
2 Read 03-FEB-2020 09:30:11
问题:
有没有办法让数据变成这样
ID Modules1 Timestamp1 Modules2 Timestamp2
1 Delivered 02-FEB-2020 08:24:45 Read 02-FEB-2020 08:27:50
1 Delivered 03-FEB-2020 09:24:45
1 Read 03-FEB-2020 10:00:50
2 Delivered 03-FEB-2020 09:28:10 Read 03-FEB-2020 09:30:11
目标:
因此,如果有人在5分钟内阅读,那么它将加入,如果没有,数据将保持不变。您可以进行自加入,以达到以下所需结果:
With cte as
(Select t.*,
Row_number() over (partition by id, modules order by timestamp) as rn
From your_table t)
Select t1.*,
case when t1.modules = 'delivered' and t1.timestamp + interval '5' minute <= t2.timestamp
then t2.timestamp
end as timestamp2
From cte t1
left join cte t2
On (t1.rn = t2.rn and t2.modules = 'read')
Left join cte3
On (t1.rn = t3.rn and t3.modules = 'delivered')
Where t1.modules = 'delivered' or t3.timestamp + interval '5' minute > t2.timestamp
干杯 我认为这是一种缺口和孤岛问题。每个孤岛的读取或任何一行的交付延迟5分钟
with tgrp as (
select t.*,
sum(case when modules = 'Delivered' or
prev_timestamp < timestamp - interval '5' minute
then 1 else 0
end) over (partition by id order by timestamp) as grp
from (select t.*,
lag(timestamp) over (partition by id order by timestamp) as prev_timestamp
from t
) t
)
select id,
max(case when seqnum = 1 then module end) as module1,
max(case when seqnum = 1 then timestamp end) as timestamp1,
max(case when seqnum = 2 then module end) as module2,
max(case when seqnum = 2 then timestamp end) as timestamp2
from (select tgrp.*,
row_number() over (partition by id, grp order by timestamp) as seqnum
from tgrp
) tgrp
group by id, grp;
将其发布到SQLFiddle.com之类的网站上,并附上一个模式和一些示例数据。然后,在你的问题中加入一个链接。这样,你更有可能得到回复。另外,模块的ID在您的连接中也很重要吗?如果没有这一点,问题就变得模棱两可了。我会尝试以下几种方法:1使用子查询将它们拆分为仅传递和仅读取,然后在id和read.time之间完全外部连接delivered.time和delivered.time+5min,如果传递的值为null,则使用读取值。2使用按ID划分的滞后/超前分析功能查看当前行;case当当前行被传递时,则“y”作为keep,case当读取下一行时,并在5分钟内将这些读取的列值固定到该行上。当当前行和前一行被读取超过5分钟,然后“y”作为keep-else“n”,则将所有内容包装在WHERE-keep='y'中
select t.id, t.module, t.timestamp,
(case when t.next_module = 'Read' and
t.next_timestamp < t.timestamp + interval '5' minute
then t.next_module
end) as module2,
(case when t.next_module = 'Read' and
t.next_timestamp < t.timestamp + interval '5' minute
then t.next_timestamp
end) as timestamp2
from (select t.*,
lead(module) over (partition by id order by timestamp) as next_module,
lead(timestamp) over (partition by id order by timestamp) as next_timestamp
from t
) t
where module = 'Delivery' or
(next_timestamp > timestamp + interval '5' minute)