0

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.

4
  • What are you trying to do? Commented Nov 3, 2014 at 10:53
  • What do you want to do? Commented Nov 3, 2014 at 10:53
  • @DavidG would get zero for "AssignmentSubmitted" to studentid 2 and studentid 1.Now it returns one for studentid 2. Commented Nov 3, 2014 at 10:55
  • @wewesthemenace would get zero for "AssignmentSubmitted" to studentid 2 and studentid 1. Now it returns one for studentid 2. Commented Nov 3, 2014 at 10:58

4 Answers 4

2

First, this is your query:

select s.StudentId, s.Studentname,
       (case (select top 1 Assignmentstatus
              from Assignment a2
              where a2.StudentId = s.StudentId
              order by date desc
             )
            when 0 then 1 else 0
        end) as AssignmentSubmitted 
from student s left join
     Assignment a 
     on a.studentId = s.StudentId;

The outer join to assignment is not necessary, so you probably really want:

select s.StudentId, s.Studentname,
       (case (select top 1 Assignmentstatus
              from Assignment a2
              where a2.StudentId = s.StudentId
              order by date desc
             )
            when 0 then 1 else 0
        end) as AssignmentSubmitted 
from student s;

Your inner join is comparing the most recent value of AssignmentStatus in Assignment for a given student to 0. For neither student is the most recent status 0. In one case it is "1". In the other "Null", so it will always return 0. A SQL Fiddle is here.

Presumably, you want the status if available. I would be inclined to write this query using outer apply:

select s.StudentId, s.Studentname, coalesce(a.Assignmentstatus, 0) as Assignmentsubmitted
from student s outer apply
     (select top 1 Assignmentstatus
      from Assignment a2
      where a2.StudentId = s.StudentId
      order by date desc
     ) a;

You can use a case instead of coalesce() if the actual logic is more complicated.

Sign up to request clarification or add additional context in comments.

Comments

1

You don't need to use a CASE statement. Instead you can replace NULL with 0 using ISNULL:

SELECT Student.StudentId,
       Student.Studentname,
       ISNULL(Assignment.AssignmentStatus, 0) AS AssignmentStatus
FROM Student 
LEFT JOIN Assignment
    ON Assignment.StudentId = Student.StudentId

1 Comment

If I have one more status '2' then I can't proceed with ISNULL(Assignment.AssignmentStatus, 0) AS AssignmentStatus.Can you please have a look
0

It seems you that you want to get the latest Assignmentstatus to any Student, don't you?

How about like that?

select t1.Studentname, 
       t2.Assignmentstatus, 
       t2.date 
  from table1 t1, table2 t2
 where t1.StudentId = t2.StudentId 
   and t2.date = (Select max(date) from table2 where StudentId = t1.StudentId)

http://www.sqlfiddle.com/#!2/5af1a1/1

Comments

0

Assuming AssignmentStatus only ever increases (e.g. doesn't ever go back to 'submitted' after being returned) then you could use something like:

SELECT s.StudentId
     , s.Studentname
     , ISNULL(MAX(a.AssignmentStatus), 0) AS AssignmentStatus
FROM Student s
     LEFT JOIN Assignment a ON a.StudentId = s.StudentId
group by s.StudentId, s.Studentname

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.