I am writing a query for retrieving data from two tables. The tables have data like this:
Table1:
StudentId Studentname
---------------------
1 test
2 test1
Table2:
StudentId Assignmentstatus date
--------------------------------------
1 0 01/01/2014
1 1 02/01/2014
status 1 means assignment submitted, 2 means returned after verification.
While joining the table
select
student.StudentId, student.Studentname,
case (select top 1 Assignmentstatus
from Assignment
where Assignment.StudentId = student.StudentId
order by date desc) when 0 then 1 else 0 end as AssignmentSubmitted
from
student
left join
Assignment on Assignment.studentId = Student.StudentId.
It returns 1 for StudentId 2 also.