我有一张桌子A
ID SALN INDICATOR
1 100 A
2 100 B
3 200 A
4 200 C
5 100 c
6 300 B
7 200 D
我需要在SALN分组后从表A中提取SALN,而该组只有指标A,B,C
SALN
100
请告诉我这个
的SQL答案 0 :(得分:1)
select saln
from A
where indicator in('A','B','C')
group by saln
having sum(indicator) = 100
答案 1 :(得分:0)
select saln
from your_table
group by saln
having sum(indicator not in ('A','B','C')) = 0
如果您还需要检查SALN
是否存在A,B和C,请添加
and sum(indicator = 'A') > 0
and sum(indicator = 'B') > 0
and sum(indicator = 'C') > 0
到查询结尾。
答案 2 :(得分:0)
如果你想要这三个值,那么你可以这样做:
select saln
from A
where indicator in ('A', 'B', 'C')
group by saln
having count(*) = 3;
如果可能有重复项,请使用:
having count(distinct indicator) = 3
如果你想要A,B和C而没有其他值,那么一种方法是:
select saln
from A
group by saln
having count(distinct case when indicator in ('A', 'B', 'C') then indicator end) = 3;