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:
Key1 Key2 NonKey1 NonKey2
k1 k2 nk1 nk2
k1 k2 null nk2
k1 k2 nk1 null
For example 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.)
other method:
select distinct key1, key2
from MyTable
where nonkey2 is not null and nonkey3 is not null
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.