sql - SQL:如何在不同列之间查找空值
问题描述
从其关联商店在多个国家/地区的就业值不大于 0 的公司列表中,我想检索在就业国家以外的国家/地区具有空就业值的 id。在下面的情况下,我只想检索 id A。这是一个 oracle 数据库。
表结构
公司
| id | store | country| employment
---------------------------------
|A | 1 | US | 0 |
---------------------------------
|A | 2 | US | 9 |
---------------------------------
|A | 3 | DE | null |
---------------------------------
|B | 4 | US | 0 |
---------------------------------
|B | 5 | DE | null |
---------------------------------
|B | 6 | DE | 4 |
---------------------------------
|B | 7 | DE | 4 |
---------------------------------
*请原谅我不知道如何在问题中以更好的格式呈现表格结构。
解决方案
这是你想要的吗?
select distinct id
from company c
group by id, country
having count(employment) <> count(*) and -- there is at least one NULL value
count(employment) > 0 ; -- there is at least one not NULL value