我的数据库有三个表,
Studentdata
列studentid , studentname
Assessmentdata
列studentid, attemptedondate
Activitydata
列studentid, date
每个表格行每天都会使用当前时间戳更新。
在识别方面需要帮助,Mostrecentdate
列应该在比较来自两个不同表格和两个不同列的这些列attemptedondate
和date
之后获取日期。
预期输出应为,
Studentid studentname mostrecentdate
1 abc 2013-06-01 12:05
2 def 2013-05-02 02:03
我目前的查询是:
select
S.StudentId, S.StudentAccountName,
Max(D.attemptedondate, A.date) as MOSTRECENTTIME
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
order by
MOSTRECENTTIME desc
答案 0 :(得分:2)
您应该可以使用CASE
:
select S.StudentId,
S.StudentAccountName,
CASE WHEN Max(D.attemptedondate) >= Max(A.date) THEN Max(D.attemptedondate)
ELSE Max(A.date) END
as MOSTRECENTTIME
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