微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如果存在另一行相同但没有“ 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。