Yassin Hajaj Yassin Hajaj - 2 months ago 8
SQL Question

SQL : Check if result = number for each id

I have this sort of structure

ID STATUS
1 FIRSTSTAT
2 FIRSTSTAT
3 FIRSTSTAT
1 SECSTAT
3 SECSTAT
3 THIRDSTAT
3 FOURTHSTAT
3 FIFTHSTAT


I want to get the
3
back because he has all following status (FIRSTSTAT, SECSTAT, THIRDSTAT). Do you have an idea how I could make that?

It should be done by explicitely giving the statuses because other statuses exist so
SELECT FROM WHERE = 'THIRDSTAT'
is not ok since it should have all three statuses, not only one of them.

So I guess it should be done calculating the SUM or something like that.

I tried the following but of course, it does not work :

SELECT
FROM
WHERE
AND
AND

Answer

If the number of different status values is known to always be 3:

select id
from tablename
where status in ('FIRSTSTAT', 'SECSTAT', 'THIRDSTAT')
group by id
having count(distinct status) = 3
Comments