我有两张表:一张用于初始评估,一张用于重新评估。格式是这样的:
initialAssessment
:
assessmentId
personId
assessmentDate
totalScore
Reassessments
:
assessmentId
personId
assessmentDate
totalScore
每个表包含每人 1 条记录。最近的评估可能未包含在重新评估表中。出于这个原因,我需要在两个表之间选择最近的评估以及给定人员的相应总分。我尝试了以下查询,但我得到了多行:
SELECT personId, MAX(assessmentDate) AS maxSubmittedDt, TotalScore
FROM (
SELECT personId, assessmentDate, TotalScore
FROM initialAssessment
WHERE personId = 1
UNION
SELECT personId, assessmentDate, TotalScore
FROM Reassessments
WHERE personId = 1) AS u
Group BY personId, TotalScore
Order By personId
我的问题是,如何在初始评估和重新评估表之间选择最近的评估及其对应的分数?如果重要的话,我使用的是 SQl server 2005。