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)

ここでフィドルを入手できます。

に null 以外の値を 1 つしか持てない場合はb、次のように簡略化できます。

select a, max(b) from test
group by a
于 2012-04-20T22:59:21.757 に答える
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アプローチには、より自己文書化されたロジックがあります:-)

そうすることを意識している場合は、非 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)
于 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

まあ、私はこの解決策が特に好きではありませんが、私には最も適しているようです。必要なものの説明は、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
于 2012-04-21T02:06:31.093 に答える
0

私のビューはより複雑だったので、ここに私の問題を解決した2つの回答を組み合わせて置きます

    --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 MostachoKenwarnerに感謝

于 2019-10-01T18:37:34.217 に答える
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
于 2012-04-21T03:54:07.040 に答える