来自:http://topic.csdn.net/u/20091221/22/240B2929-8F9E-430C-824C-7A4865954E33.html
在学生成绩表中,显示存在有85分以上成绩的课程号,并且统计各门课程不及格人数在10人以上的课程数量
use tempdb if object_id('tb') is not null drop table tb go create table tb([stud_id] varchar(50),[course_id] INT,[grade] INT) insert into tb select '001',1,45 union all select '002',1,88 union all select '003',1,90 union all select '001',2,30 union all select '002',2,50 union all select '003',2,30 union all select '004',2,30 union all select '005',2,50 union all select '006',2,55 union all select '007',2,30 union all select '008',2,95 union all select '009',2,30 union all select '010',2,44 union all select '011',2,33 union all select '012',2,30 go --select * from tb select course_id from tb group by course_id having max(grade) > 85 /* course_id ----------- 1 2 */ intersect select course_id from tb group by course_id having sum(case when grade < 60 then 1 else 0 end) > 10 /* course_id 不及格人数 ----------- ----------- 2 11 (1 行受影响) */ --同时满足 select course_id from tb group by course_id having max(grade) > 85 intersect select course_id from tb group by course_id having sum(case when grade < 60 then 1 else 0 end) > 10 /* course_id ----------- 2 */