[英]Oracle SQL - Find Consecutive Values on Consecutive Dates
我必须编写一个查询来查找一个值(引用列标志)的3个或更多连续出现,并且它必须在连续的日期发生。如果该出现不在连续的日期,则该查询不应选择值。例如
COLUMN ID DATE FLAG
100 10-JUL-2015 Y
100 11-JUL-2015 Y
100 12-JUL-2015 Y
100 13-JUL-2015 N
100 14-JUL-2015 Y
100 15-JUL-2015 Y
100 16-JUL-2015 N
100 17-JUL-2015 Y
100 18-JUL-2015 Y
100 19-JUL-2015 Y
100 20-JUL-2015 Y
100 21-JUL-2015 Y
OUTPUT
COLUMN ID DATE FLAG
100 10-JUL-2015 Y
100 11-JUL-2015 Y
100 12-JUL-2015 Y
100 17-JUL-2015 Y
100 18-JUL-2015 Y
100 19-JUL-2015 Y
100 20-JUL-2015 Y
100 21-JUL-2015 Y
在Oracle SQL中实现此目标的任何想法。 我正在尝试使用LAG和LEAD之类的分析功能,但无法完成此任务。
您可以使用一个非常方便的技巧来做到这一点。 可以使用row_number()
的差来计算连续值的组。 然后,您需要获取每个组的计数并选择与您的条件相匹配的组:
select t.*
from (select t.*, count(*) over (partition by id, flag, grp) as cnt
from (select t.*,
(row_number() over (partition by id order by date) -
row_number() over (partition by id, flag order by date)
) as grp
from table t
) t
) t
where cnt >= 3;
严格来说,您不需要row_numbers()
的区别。 假设您的日期没有时间成分,则以下内容也将足够:
select t.*
from (select t.*, count(*) over (partition by id, flag, grp) as cnt
from (select t.*,
(date -
row_number() over (partition by id, flag order by date)
) as grp
from table t
) t
) t
where cnt >= 3;
你可以试试看 这利用了递归公用表表达式和lead
分析功能。
with x as
(select id, mydate, flag from table1 where flag = 'Y')
, y as(select id, mydate, lead(mydate) over(order by mydate) as nxt,flag from x)
, z as (select id, mydate, nxt, lead(nxt) over(order by nxt) as nxt_1,flag from y)
select distinct t.id, t.mydate,t.flag from z
join x t on z.id = t.id
and (t.mydate = z.mydate or t.mydate = z.nxt or t.mydate = z.nxt_1)
where z.nxt-z.mydate = 1 and z.nxt_1-z.nxt =1
order by t.mydate
带测试数据的SQLfiddle: http ://sqlfiddle.com/#!4/9bbed/1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.