分组统计过滤数据的问题

求一个sql,数据如下:

  •  id       no       dep   name
  • 001    200     2004    aa
  • 002    201     2004    aa
  • 003    212     2004    aa
  • 004    252     2004    aa
  • 005    123     2005    bb
  • 006    222     2005    bb
  • 007    212     2005    bb
  • 008    212     2006    cc
  • 009    111     2006    cc
  • 010    231     2006    cc
  • 011    201     2006    cc


我的需求是:
按dep号分组,只要一组中不存在no为201的数据,这一组的dep就是我想要得到的结果。
也就是说以上面的数据为例,我要得到的dep是2005。

望高手们能不吝赐教,小弟感激不尽!

 


问题补充:
引用
select dep from t group by dep having dep not in (select dep from t where no='201')
foy (初级程序员) 2009-02-19 采纳为答案

首先,谢谢!
但我的数据量比较大,能不能再优化一下。

select dep from t group by dep having dep not in (select dep from t where no='201')

select dep t group by dep having dep not in (select dep from t where no='201')
试试看这个对没

在no上建立索引。