从图像中可以看到。第5行和第6行在其他方面相同,但第5行的值为“ NULL”。所以我想过滤掉第5行,但保留第6行。
然后,还有几行具有“ NULL”值,但我也想保留这些值。那我该怎么做呢?如何保留具有“ NULL”值的单行/唯一行,并同时过滤出具有相同对应物但对应物具有“ NULL”值的行?
我尝试使用-function合并分组,但尚未得到想要的结果。
答案 0 :(得分:0)
您可以使用not exists
:
select t.*
from mytable t
where
date_updated is not null
or not exists (
select 1
from mytable t1
where
t1.state = t.state
and t1.foo = t.foo
and t1.date_created = t.date_created
and t1 date_created is not null
)
另一个选择是窗口函数,如果您的数据库支持的话:
select *
from (
select
t.*,
max(date_updated) over(partition by state, foo, date_created) max_date_updated
from mytable t
) t
where date_updated is not null or max_date_updated is null