在SELECT DISTINCT中消除NULL的最简单方法是什么?

时间:2012-04-20 22:38:49

标签: sql sql-server

我正在处理与以下内容非常相似的查询:

CREATE TABLE #test (a char(1), b char(1))

INSERT INTO #test(a,b) VALUES 
('A',NULL),
('A','B'),
('B',NULL),
('B',NULL)

SELECT DISTINCT a,b FROM #test

DROP TABLE #test

结果是,毫不奇怪,

a   b
-------
A   NULL
A   B
B   NULL

我希望实际看到的输出是:

a   b
-------
A   B
B   NULL

也就是说,如果列在某些记录中有值但在其他记录中没有,我想抛出该列为NULL的行。但是,如果列的所有记录都有NULL值,我想保留该NULL。

在单个查询中执行此操作的最简单/最优雅的方法是什么?

我觉得如果我在星期五下午没有筋疲力尽就会很简单。

8 个答案:

答案 0 :(得分:11)

试试这个:

select distinct * from test
where b is not null or a in (
  select a from test
  group by a
  having max(b) is null)

你可以得到小提琴here

请注意,如果b中只有一个非空值,则可以简化为:

select a, max(b) from test
group by a

答案 1 :(得分:1)

试试这个:

create table test(
x char(1),
y char(1)
);

insert into test(x,y) values
('a',null),
('a','b'),
('b', null),
('b', null)

查询:

with has_all_y_null as
(
    select x
    from test
    group by x
    having sum(case when y is null then 1 end) = count(x)
)
select distinct x,y from test
where 

    (
        -- if a column has a value in some records but not in others,
        x not in (select x from has_all_y_null) 

        -- I want to throw out the row with NULL
        and y is not null 
    )
    or 
    -- However, if a column has a NULL value for all records, 
    -- I want to preserve that NULL
    (x in (select x from has_all_y_null))

order by x,y

输出:

 X    Y
 A    B
 B    NULL

实时测试:http://sqlfiddle.com/#!3/259d6/16

修改

看到Mosty's answer,我简化了我的代码:

with has_all_y_null as
(
    select x
    from test
    group by x

    -- having sum(case when y is null then 1 end) = count(x) 
    -- should have thought of this instead of the code above. Mosty's logic is good:
    having max(y) is null
)
select distinct x,y from test
where 
    y is not null
    or 
    (x in (select x from has_all_y_null))
order by x,y

我更喜欢CTE方法,它有一个更自我记录的逻辑: - )

如果你有意识的话,你也可以把文件放在非CTE方法上:

select distinct * from test
where b is not null or a in 
  ( -- has all b null
  select a from test
  group by a
  having max(b) is null)

答案 2 :(得分:1)

;WITH CTE
    AS
    (
    SELECT DISTINCT * FROM #test
    )
    SELECT a,b
    FROM CTE        
    ORDER BY CASE WHEN b IS NULL THEN 9999 ELSE b END ; 

答案 3 :(得分:0)

SELECT DISTINCT t.a, t.b
FROM   #test t
WHERE  b IS NOT NULL
OR     NOT EXISTS (SELECT 1 FROM #test u WHERE t.a = u.a AND u.b IS NOT NULL)
ORDER BY t.a, t.b

答案 4 :(得分:0)

这是一个非常奇怪的要求。我想知道你是怎么需要的。

SELECT DISTINCT a, b
FROM   test t
WHERE  NOT ( b IS  NULL
          AND EXISTS 
              ( SELECT * 
                FROM test ta 
                WHERE ta.a = t.a 
                  AND ta.b IS NOT NULL
               ) 
             )
  AND  NOT ( a IS  NULL
          AND EXISTS 
              ( SELECT * 
                FROM test tb 
                WHERE tb.b = t.b 
                  AND tb.a IS NOT NULL
               ) 
             )

答案 5 :(得分:0)

嗯,我不是特别喜欢这个解决方案,但它似乎对我来说最合适。请注意,您对所需内容的描述与LEFT JOIN的内容完全相同,因此:

SELECT DISTINCT a.a, b.b
FROM #test a
    LEFT JOIN #test b ON a.a = b.a
        AND b.b IS NOT NULL

答案 6 :(得分:0)

SELECT a,b FROM #test t where b is not null
union
SELECT a,b FROM #test t where b is null
and not exists(select 1 from #test where a=t.a and b is not null)

结果:

a    b
---- ----
A    B
B    NULL

答案 7 :(得分:0)

由于我的视图更复杂,我将把两个答案混合在一起解决了我的问题

    --IdCompe int,
    --Nome varchar(30),
    --IdVanBanco int,
    --IdVan int
    --FlagAtivo bit,
    --FlagPrincipal bit

    select IdCompe
           , Nome
           , max(IdVanBanco)
           , max(IdVan)
           , CAST(MAX(CAST(FlagAtivo as INT)) AS BIT) FlagAtivo
           , CAST(MAX(CAST(FlagPrincipal as INT)) AS BIT) FlagPrincipal
    from VwVanBanco
           where IdVan = {IdVan} or IdVan is null
           group by IdCompe, Nome order by IdCompe asc

感谢mosty mostacho和           kenwarner