简体   繁体   English

在SQL Server中使用For Each

[英]Using For each In SQL Server

I have this table in my database: 我的数据库中有此表:

StudentExam : StudentExam

studentID, ExamID, EarnedMarks, Status

I want a query that returns (foreach examID check status, if 'done' return ExamID else return nothing.) 我想返回一个查询(foreach auditID检查状态,如果“完成”则返回ExamID,否则不返回任何内容。)

That means every student had attended the exam. 这意味着每个学生都参加了考试。

Thanks 谢谢

To bring back all ExamId where all it's associated status are Done one way would be 要带回所有与它的所有关联状态都Done ExamId,一种方法是

SELECT ExamId 
FROM   StudentExam 
GROUP  BY ExamId 
HAVING COUNT(*) = SUM(CASE 
                        WHEN Status = 'DONE' THEN 1 
                      END) 

Or another 或其他

SELECT ExamId 
FROM   StudentExam 
WHERE  Status = 'DONE' 
EXCEPT 
SELECT ExamId 
FROM   StudentExam 
WHERE  Status <> 'DONE' 
        OR Status IS NULL 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM