如何在oraclesql中查找和过滤同一表上的数据

h9vpoimq  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(267)

在sql中,我们需要从表中过滤不必要的数据:
案例1:如果两个ID相同,dod不为空,则需要记录
情况2:如果有一个id,dod不为空,则需要记录
情况3:如果两个ID相同,并且其中任何一个的dod为空,则不需要记录

非常感谢你的帮助。
谢谢

anauzrmj

anauzrmj1#

您可以为此使用分析函数:

select t.*
from (
    select 
        t.*, 
        sum(case when dod is null then 1 else 0 end) over(partition by id) no_nulls
    from mytable t
) t
where no_nulls = 0

注意,这也排除了没有重复记录的记录 id 但是谁的 dodnull (您没有描述如何处理这些问题)。
你也可以用 not exists (可以方便地变成 delete 声明(如需要):

select t.*
from mytable t
where not exists(select 1 from mytable t1 where t1.id = t.id and t1.dod is null)
where no_nulls = 0

相关问题