查询层次查询

时间:2019-03-18 20:20:12

标签: sql oracle

我想分析我的数据集以发现数据差异。

我的采样日期设置:

id  status       stdate         enddate
1   new          01-JUL-17  31-JUL-17
1   process      01-OCT-17  31-DEC-18
1   new          01-JAN-19  31-JAN-19--- issue
2   new          01-SEP-14  31-JAN-15
2   process      01-JUN-16  30-NOV-17
2   complete     01-DEC-17  31-DEC-18
....
....

我想找出其中有多少个ID的结果状态早于当前状态。状态序列的顺序应为NEW-PROCESS-COMPLETE。因此,我想报告所有ID,其中最新状态已反转为较早状态。

1 个答案:

答案 0 :(得分:3)

您可以使用LAG()函数来查找有问题的行,如:

with x (id, status, stdate, enddate, 
        prev_id, prev_status, prev_stdate, prev_enddate) as (
  select
    id, 
    status,
    stdate,
    enddate,
    lag(id) over(partition by id order by stdate),
    lag(status) over(partition by id order by stdate),
    lag(stdate) over(partition by id order by stdate),
    lag(enddate) over(partition by id order by stdate)
  from my_table
)
select * from x
where status = 'new' and prev_status in ('process', 'complete')
   or status = 'process' and prev_status = 'complete'

注意:我假设您只需要在同一ID的行之间进行比较。