【更新了下】SQL分组查询,包含一个字段但同时不包含另外一个字段,刚描述的不清楚,希望不要有子查询谢谢

请各位大侠支个招,万分感谢
table表:有如下2列

Number view

10      A
10      B
10      C

11      A
11      C

12      B
12      C

13      A

条件:查询View列,含有A同时不含有B
返回值:Number

如:上表返回结果 11,13

 
 

这里只是举个个小例子,实际数据量很大,如果用子查询,就会执行两遍
最好不要用子查询的

select number from (select * from table where view != 'B') t where view = 'A'
先去掉包含B的
在选出包含A的

select number from table group by number having sum(case when view='A' then 1 else 0 end)>0 and sum(case when view='B' then 1 else 0 end)=0

select distinct number
from tablename

where view ='A' and View !=B ;

先过滤掉含有B 的id 与 A的id相等的数据,在按照abc 分组,同时将id列 多行合并,可得出结果 a 11.13

select Number from table where view not in (select Number from table where view='B')

select a.numberstr from test a where a.viewstr='A' and a.numberstr not in (select b.numberstr from test b where b.viewstr='B')

select a.number num from table a where not exits(select * from table b where a.view='A' )