1

我的数据库有三个表,

  • 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
4

1 回答 1

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
于 2013-06-07T03:12:52.193 回答