我有一个表学生,有以下几列
StudentId和SemesterId以及ExamYearId
*
*
*3*3。
*
*2*
*
*1
*4*。
8%*1
8元人民币*2
我想要一个查询来获得所有这样的学生,这些学生没有semesterid=3和examyearid=3的数据,但是同一个学生应该有semesterid=2的数据。
在这种情况下,它应该返回studentid=8,因为studentid=8有semesterid=2和examyearid=2的数据,但没有examyearid=3和semesterid=3的数据。
基本上,我希望能够输入semesterid和examyearid,并找出哪些studentid没有填充该semesterid和examyearid的数据。
发布于 2015-05-19 02:49:33
DECLARE @SemesterId INT
,@ExamYearId INT
SET @SemesterId = 3;
SET @ExamYearId = 3;
SELECT *
FROM TableName t
WHERE EXISTS (SELECT 1
FROM TableName
WHERE SemesterId = @SemesterId - 1
AND ExamYearId = @ExamYearId - 1
AND t.StudentId = StudentId )
AND NOT EXISTS (SELECT 1
FROM TableName
WHERE SemesterId = @SemesterId
AND ExamYearId = @ExamYearId
AND t.StudentId = StudentId ) 发布于 2015-05-19 06:29:14
SELECT st1.*
FROM Student st1
left join Student st2
on st1.id = st2.id
and st1.Semesterid=2
and st2.Semesterid=3
and st2.Examyearid=3
where st2.id is nullhttps://stackoverflow.com/questions/30310637
复制相似问题