Skip to content
Advertisement

Selecting distinct values

Trying to get a selection of records where if COUNT ([WHO]) > 1 then select [WHO] where the [LIT] <> 0.

Example if [WHO] = ‘12082132’ , ‘12082132’ and [LIT] = 0, then ignore [WHO].

WHO        LIT  COUNT
3517015     7   1
3523687     0   1
12057744    0   2
12058316    7   1
12059820    7   1
12082132    2   2
12082132    5   1
12082132    1   3
12082132    14  1
12082132    0   1

I have tried grouping, case statements, but I think I am trying to get something that is not possible. Any help will do.

Expected results

 WHO           LIT  COUNT
    3517015     7   1
    12058316    7   1
    12059820    7   1

Advertisement

Answer

You can use not exists & use window function :

select t.*, count(*) over (partition by t.who) as cnt
from table t
where not exists (select 1 
                  from table t1
                  where t1.who = t.who and t1.lit = 0
                 );
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement