简体   繁体   English

SQL在多列中找到至少一个不为空

[英]SQL to find atleast one not nulls in multiple columns

I have a table with few non key attributes. 我的桌子上有一些非关键属性。 I would like to know how can I write a query to find the key columns where I can find not nulls in any one row of these. 我想知道如何编写查询以查找键列,在这些键列中任何一行我都找不到空值。

For example 1: 例如1:

Key1 Key2 NonKey1 NonKey2    
k1   k2   nk1      nk2    
k1   k2   null     nk2    
k1   k2   nk1      null  

For example 2: 例如2:

Key1 Key2 NonKey1 NonKey2 NonKey3    
k1   k2   null     nk2     nk3
k1   k2   nk1      nk2     null

Expected: 预期:

Key1 Key2 NonKey1 NonKey2
k1   k2   nk1      nk2

Do you simply want to grab one value per non-key column? 您是否只想为每个非键列获取一个值?

select key1, key2, max(nonkey1), max(nonkey2)
from mytable
group by key1, key2;

Something like this: 像这样:

    select key1, key2, max(nonkey2), max(nonkey3)
    from MyTable
    having max(nonkey2) is not null or max(nonkey3) is not null
    group by key1, key2

(This is SQL Server, but you get the idea.) (这是SQL Server,但您明白了。)

other method: 其他方法:

select distinct key1, key2
from MyTable
where nonkey2 is not null and nonkey3 is not null

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

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