27

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

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。

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

我有一种感觉,如果我在星期五下午没有筋疲力尽,这将很简单。

4

8 回答 8

21

试试这个:

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

你可以在这里得到小提琴。

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

select a, max(b) from test
group by a
于 2012-04-20T22:59:21.757 回答
2
;WITH CTE
    AS
    (
    SELECT DISTINCT * FROM #test
    )
    SELECT a,b
    FROM CTE        
    ORDER BY CASE WHEN b IS NULL THEN 9999 ELSE b END ; 
于 2017-03-09T12:29:12.400 回答
1

Try this:

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

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

Query:

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

Output:

 X    Y
 A    B
 B    NULL

Live test: http://sqlfiddle.com/#!3/259d6/16

EDIT

Seeing Mosty's answer, I simplified my code:

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

I just prefer CTE approach, it has a more self-documenting logic :-)

You can also put documentation on non-CTE approach, if you are conscious of doing so:

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)
于 2012-04-21T02:48:43.017 回答
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
于 2012-04-20T22:43:10.363 回答
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
               ) 
             )
于 2012-04-20T22:58:47.003 回答
0

Well, I don't particularly like this solution, but it seems the most appropriate to me. Note that your description of what you want sounds exactly like what you get with a LEFT JOIN, so:

SELECT DISTINCT a.a, b.b
FROM #test a
    LEFT JOIN #test b ON a.a = b.a
        AND b.b IS NOT NULL
于 2012-04-21T02:06:31.093 回答
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)

Result:

a    b
---- ----
A    B
B    NULL
于 2012-04-21T03:54:07.040 回答
0

I'll just put here a mix of two answers that solved my issue, because my View was more complex

    --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

Thanks to mosty mostacho and kenwarner

于 2019-10-01T18:37:34.217 回答