过滤任何

时间:2014-06-17 10:56:26

标签: sql sql-server sql-server-2008

我有下表:

studentID subjectID PASS
101        1         T
102        1         F
101        2         F
101        3         T
102        2         F  

我应该提出警告if a student has not passed in ALL of the subjects he had attempted.

因此,在上述情况中 - the query should return only 102.

Note - it should also not return 103,who has not attempted any of the subjects(assuming we are joining this with the student table).

2 个答案:

答案 0 :(得分:0)

您可以通过以下方式获取此类学生的列表:

select studentId
from table t
group by studentId
having max(PASS) = 0;

如何提高警报是另一回事。你应该安排一份工作去做。

编辑:

好吧,PASS的定义自我原来的答案以后发生了变化。以下是字符字段的修订版:

select studentId
from table t
group by studentId
having sum(case when PASS = 'T' then 1 else 0 end) = 0;

答案 1 :(得分:0)

列出所有参加科目失败的学生

试试这个!

;with cte as
(
select studentID, case when pass='T' then 1 else 0 end as xx from #t
)
select studentID from cte  group by studentID having sum(xx)=0

或没有CTE

select studentID from 
(
select studentID, case when pass='T' then 1 else 0 end as xx from #t
)x
group by studentID having sum(xx)=0

Fiddle demo