Skip to content
Advertisement

SQL count row where progres not done

Need some help, dont know the keyword of this problem to search online

I want to count some progres that isn’t done

table-example

when progres has step3 then its not counted

desired result from that example is 2, im trying to do it alone, and it doesnt work

help is needed, Thanks Ahead

Advertisement

Answer

One method uses count(distinct) and filters in the where clause:

select count(distinct progres)
from t
where not exists (select 1 from t t2 where t2.progres = t.progres and t2.step = 'step3');

Another fun way uses a difference:

select count(distinct progres) - count(distinct case when step = 'step3' then progres end)
from t;

If 'step3' can appear at most once per progres, the above can be simplified to:

select count(distinct progres) - sum(step = 'step3')
from t;

Or using set operations:

select count(*)
from ((select progres from t)
      except  -- removes duplicates
      (select progres from t where step = 'step3')
     ) t;
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement