从表中选择数据仅为null

时间:2015-07-06 10:54:19

标签: sql oracle

我只需选择美国数据不为空的数据,而其他国家则为空。

field1   field2    field3
A        US        A
A        DE        null
A        IN        null
A        JP        null
B        US        null
B        DE        B
B        IN        C
B        JP        null

4 个答案:

答案 0 :(得分:3)

查询应该非常接近要求:

j = i

答案 1 :(得分:2)

select field1 from 
(
select field1,
        field2,
        count(*) over (partition by field1) not_null_count
        from tablename
        where field3 is not null
        ) 
where field2 = 'US' 
and not_null_count = 1

答案 2 :(得分:0)

您只需在谓词中正确使用括号

SELECT * 
FROM  table_name
WHERE (field2 =  'US'  AND field3 IS NOT NULL)
OR    (field2 <> 'US'  AND field3 IS NULL)

答案 3 :(得分:0)

您可以使用以下查询

Select * from table_name where field2="US" && field3 IS NOT NULL OR field2!="US" && field3 IS NULL;