幾乎相同IDEEA作爲民主黨,有一點不同:
select * from (
select eventtime, speed, rnk, new_rnk,
rnk - new_rnk,
max(rnk) over (partition by speed, new_rnk-rnk) -
min(rnk) over (partition by speed, new_rnk-rnk) + 1 as no_consec
from (
select eventtime, rnk, speed,
row_number() over (partition by speed order by eventtime) as new_rnk
from (
select eventtime, speed,
row_number() over (order by eventtime) as rnk
from a
) a
where a.speed < 5
)
order by eventtime
)
where no_consec >= 2;
5是限速和2是分鐘數目的連續事件。 爲了簡化寫入創建數據庫,我將日期設置爲數字。
SQLFIDDLE
編輯:
要回答的意見,我在第一個內部查詢添加三列。要獲得第一行,您需要將WHERE子句添加一個pos_in_group = 1
,距離在您的手指上。
SQLFIDDLE
select eventtime, speed, min_date, max_date, pos_in_group
from (
select eventtime, speed, rnk, new_rnk,
rnk - new_rnk,
row_number() over (partition by speed, new_rnk-rnk order by eventtime) pos_in_group,
min(eventtime) over (partition by speed, new_rnk-rnk) min_date,
max(eventtime) over (partition by speed, new_rnk-rnk) max_date,
max(rnk) over (partition by speed, new_rnk-rnk) -
min(rnk) over (partition by speed, new_rnk-rnk) + 1 as no_consec
from (
select eventtime, rnk, speed,
row_number() over (partition by speed order by eventtime) as new_rnk
from (
select eventtime, speed,
row_number() over (order by eventtime) as rnk
from a
) a
where a.speed < 5
)
order by eventtime
)
where no_consec > 1;
難道這些數據被一些其他領域的組合在一起?比如'thingy_id',還是這兩列真的只有兩列? – MatBailie
@Dems - 還有其他列將幫助將結果集限制在10k/100k行 - 但我所尋找的解決方案不能比上面列出的基本示例進一步分組。沒有什麼能夠幫助你了,它可以根據事件時間和速度字段識別下一個/上一行。 – Faraday