My database has three tables,
Studentdata with columns studentid , studentname
Assessmentdata with columns studentid, attemptedondate
Activitydata with columns studentid, date
And each table row will be updated every day with the current timestamp.
Need help in identifying, Mostrecentdate column should get the date after comparing both these columns attemptedondate and date from two different tables and two different columns.if the column is null in both the activitydata and assessmentdata then print the student id with in the expected output with mostrecentdate is null as shown.
Left outer join should be with studentdata table , where should I write this ?
Expected output should be,
Studentid studentname mostrecentdate
1 abc 2013-06-01 12:05
2 def 2013-05-02 02:03
3 kjr null
My current query is:
select S.StudentId,
S.StudentAccountName,
CASE WHEN Max(D.attemptedondate) >= Max(A.date) THEN Max(D.attemptedondate)
ELSE Max(A.date) END
as MOSTRECENTDATE
from activitydata A
join Studentdata S on A.StudentId=s.StudentID
join Assessmentdata D on S. StudentID =D. StudentID
group by S.StudentId,
S.StudentAccountName
mostrecentdatecolumn - Ravi