获取从未属于任何类别的ID

时间:2018-05-15 21:44:19

标签: sql

我有一张桌子

ID |Location|Programs|School                                                
---+--------+--------+-------
 1 |ABC     |A       | 1                                                    
 1 |ABC     |B       | 1                                                    
 1 |ABC     |C       | 1                                                    
 1 |ABC     |D       | 1                                                    
 2 |ABC     |A       | 1                                                    
 2 |ABC     |B       | 1                                                    
 2 |ABC     |C       | 1                                                    
 3 |ABC     |D       | 1                                                    
 4 |ABC     |A       | 1                                                    
 4 |ABC     |B       | 1                                                    
 4 |ABC     |C       | 1                                                    
 4 |ABC     |D       | 1                                                    
 5 |ABC     |A       | 1                                                    
 5 |ABC     |C       | 1                                                    
 5 |ABC     |D       | 1                                                    
 6 |ABC     |A       | 1                                                    
 6 |ABC     |B       | 1                                                    
 6 |ABC     |C       | 1                                                    
 7 |ABC     |A       | 1                                                    
 7 |ABC     |B       | 1                                                    
 7 |ABC     |C       | 1                                                    
 8 |ABC     |A       | 1                                                    
 8 |ABC     |B       | 1                                                    
 8 |ABC     |C       | 1                                                    
 8 |ABC     |D       | 1                                                    
 8 |ABC     |E       | 1                                                    
 9 |ABC     |B       | 1                                                    
 9 |ABC     |C       | 1                                                    
 9 |ABC     |D       | 1                                                    
 9 |ABC     |E       | 1                                                    
10 |ABC     |A       | 1                                                   
10 |ABC     |B       | 1                                                   
10 |ABC     |C       | 1                                                   
11 |ABC     |A       | 1                                                   
11 |ABC     |B       | 1                                                   

我想获取ID 6,7,10,1,任何包含D或E的ID都将被过滤。

请给我一些建议。

1 个答案:

答案 0 :(得分:0)

如果你有一个表,那么聚合很容易:

select id
from t
group by id
having sum(case when program in ('D', 'E') then 1 else 0 end) = 0;

更有可能的是,你有一张ids表,在这种情况下我会推荐not exists

select id
from ids
where not exists (select 1 from t where t.id = ids.id and t.program in ('D', 'E'));

这可以利用适当的索引,并且可以非常快。