在SQL中,我们需要从表中过滤掉不必要的数据:
情况1:如果两个ID相同且DOD不为空,则需要记录
情况2:如果单个id存在并且dod不为null,则需要记录
情况3:如果2个id相同,并且对于任何一个id都为null,则不需要记录
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9EOXJIZC5wbmcifQ==” alt =“在此处输入图像描述”>
非常感谢您的帮助。
谢谢
您可以为此使用分析功能:
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
但其dod
为null
的记录(您没有描述如何处理这些记录。)>
您还可以使用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