sql server 2012 - How to use Count for specific condition -
how can count , show how many opportunity have stage 3 dont have stage 2?
+-------+-------+ | oppid | stage | +-------+-------+ | abc | 1 | | abc | 2 | | abc | 3 | | abc | 4 | | cdf | 3 | | cdf | 4 | | efg | 1 | | efg | 2 | | efg | 3 | | hij | 2 | | hij | 3 | | lmi | 1 | | lmi | 2 | | lmi | 4 | +-------+-------+
the count result 1
+-------+-------+ | oppid | stage | +-------+-------+ | cdf | 3 | | cdf | 4 | +-------+-------+
got it, use not exists
, count distinct
in following:
select count(distinct oppid) tbl t1 not exists (select 1 tbl t2 t1.oppid = t2.oppid , t2.stage = 2) , t1.stage = 3
Comments
Post a Comment