如何在 Oracle SQL 中对同一个 table 进行查找和过滤数据
how to do lookup and filter data on the same table in Oracle SQL
在SQL中,我们需要从table中过滤掉不需要的数据:
情况1:如果2个ID相同且DOD不为空则需要记录
情况2:如果存在单个id且dod不为空则需要记录
情况3:如果2个id相同,其中任何一个DOD为null,则不需要记录
非常感谢您的帮助。
谢谢
您可以为此使用分析函数:
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
在SQL中,我们需要从table中过滤掉不需要的数据:
情况1:如果2个ID相同且DOD不为空则需要记录
情况2:如果存在单个id且dod不为空则需要记录
情况3:如果2个id相同,其中任何一个DOD为null,则不需要记录
非常感谢您的帮助。
谢谢
您可以为此使用分析函数:
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