MS Access中的FIRST_VALUE

时间:2019-02-21 21:41:40

标签: sql ms-access

我有一个DB2查询。谁能帮助我在MS Access中编写此查询。表中突出显示的行是预期的输出。谢谢。

SELECT s.Student_ID,
first_value(DT_EFFECTIVE) OVER (PARTITION BY S.Student_ID ORDER BY DT_EFFECTIVE DESC) AS DT_EFFECTIVE, 
first_value(Student_Goal) OVER (PARTITION BY S.Student_ID ORDER BY DT_EFFECTIVE DESC) AS Student_Goal
FROM Student AS S
LEFT JOIN St_Goal AS G ON  s.Student_ID = G.Student_ID
WHERE S.Student_ID = 12345

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以通过相关子查询来做到这一点:

SELECT s.Student_ID,
       (SELECT TOP (1) sg2.DT_EFFECTIVE
        FROM St_Goal as sg2
        WHERE sg2.Student_ID = S.Student_ID
        ORDER BY sg2.DT_EFFECTIVE DESC
       ) as DT_EFFECTIVE, 
       (SELECT TOP (1) DT_EFFECTIVE
        FROM St_Goal as sg2
        WHERE sg2.Student_ID = S.Student_ID
        ORDER BY sg2.DT_EFFECTIVE DESC
       ) as Student_Goal
FROM Student AS S
WHERE S.Student_ID = 12345;

除非您想要一堆重复项,否则在外部查询中不需要引用St_Goal