繁体   English   中英

如何 select 只有那些行,其中一列可以有 null 或没有 null 相同 ID 的值

[英]How to select only those rows where one column can have null or not null values for the same id

有人可以帮助我如何 select 只有没有 null 值的列的行,其中该列可以有 null 或没有 null 相同 ID 的值

我有一张这样的桌子

在此处输入图像描述

我需要这样的 output

在此处输入图像描述

你的描述不是很清楚。 如果我确实正确理解了您的要求,那么每个col1只需要 1 行。 选择值最多不是 null 的行?

select *
from
(
    select *, rn = row_number() over (partition by col1
                                      order by case when col2 is not null then 1 else 0 end
                                      +        case when col3 is not null then 1 else 0 end
                                      +        case when col4 is not null then 1 else 0 end 
                                      desc)
    from   a_table
) d
where rn = 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM