Input:
id group_id type_id
1 1 aaaaa
2 1 BAD
3 2 bbbbb
4 2 ccccc
5 3 ddddd
6 3 eeeee
7 3 aaaaa
group_id
s which consist only of a members for which type_id <> 'BAD'
. A whole group with at least one BAD
member should be excluded NOT EXISTS
or views or T-SQL inline functions) is not allowed! except
is not allowed! Any solutions which trick the rules above are appreciated. Any RDBMS is ok.
Bad example solution producing correct results, (using except
):
select distinct group_id
from input
except
select group_id
from input
where type_id = 'bad'
group by group_id, type_id
Output:
group_id
2
3
I would just use group by
and having
:
select group_id
from input
group by group_id
having min(type_id) = 'good' and max(type_id) = min(type_id);
This particular version assumes that type_id
(as in the question) does not take on NULL
values. It is easily modified to take that into account.
EDIT:
If you are looking for one bad, then just do:
select group_id
from input
where type_id = 'bad'
group by group_id;
Group by group_id
and count occurrences of 'BAD'
:
select group_id
from mytable
group by group_id
having count(case when type_id = 'BAD' then 'count me' end) = 0;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.