如果存在另一行相同但没有“ NULL”值的行,如何过滤掉“ NULL”值行?

问题描述

从图像中可以看到。第5行和第6行在其他方面相同,但第5行的值为“ NULL”。所以我想过滤掉第5行,但保留第6行。

然后,还有几行具有“ NULL”值,但我也想保留这些值。那我该怎么做呢?如何保留具有“ NULL”值的单行/唯一行,并同时过滤出具有相同对应物但对应物具有“ NULL”值的行?

我尝试使用-function合并分组,但尚未得到想要的结果。

enter image description here

解决方法

您可以使用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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...