查找该列中其他两个值之间的一列中包含3个以上值的所有记录

时间:2017-04-18 11:36:25

标签: sql oracle

例如我有表:

COL_0   COL_1   COL_2
11111   BBBB    RECORD DELETED
11111   BBBB    RECORD ADDED
11111   AA      lalala
11111   BBBB    RECORD ADDED
11111   AA      lalala
11111   CCCCCC  RECORD ADDED
11111   AA      lalala
11111   BBBB    RECORD ADDED
11111   AA      lalala
11111   BBBB    RECORD ADDED
11111   CCCCCC  RECORD ADDED
11111   AA      lalala
11111   BBBB    RECORD DELETED
11111   AA      lalala
11111   BBBB    RECORD ADDED
11111   BBBB    RECORD ADDED
11111   AA      lalala
11111   CCCCCC  RECORD ADDED
11111   AA      lalala
11111   BBBB    RECORD DELETED
11111   AA      lalala
11111   BBBB    RECORD ADDED
11111   AA      lalala
11111   BBBB    RECORD ADDED
11111   CCCCCC  RECORD ADDED
11111   AA      lalala
11111   BBBB    RECORD DELETED

COL_0是id,在实际表中有许多不同的id。 COL_1是另一个表的链接。 COL_2在其他表上运行。

无法改变。问题是应用程序无法正常工作并在某些情况下删除记录。现在我需要找到两个" RECORD DELETED"之间的所有id。超过3"记录添加"。有没有办法呢?

BR,

1 个答案:

答案 0 :(得分:0)

是的,你可以通过使用分析函数来做到这一点 - 假设你有一个指定排序的列。我将假设有一个dte列,其中包含此信息。

我首先要使用累计记录删除记录来识别组。然后我会计算每组中添加的记录:

select id, min(dte), max(dte)
from (select t.*,
             sum(case when col_2 = 'RECORD DELETED' then 1 else 0 end) over (partition by col_1 order by dte) as grp
      from t
     ) t
where col_2 = 'RECORD ADDED'
group by id, grp
having count(*) >= 3;

注意:这会在发生这种情况时发生。如果您真的只想要ID,那么您可以将select distinctgroup by一起使用 - 这是极少数情况下这是一个合适的解决方案:

select distinct id
from (select t.*,
             sum(case when col_2 = 'RECORD DELETED' then 1 else 0 end) over (partition by col_1 order by dte) as grp
      from t
     ) t
where col_2 = 'RECORD ADDED'
group by id, grp
having count(*) >= 3;