我有一个表,如果生成PSR,每个月为每个项目状态报告(PSR)生成一条记录。从单个表中,我需要获得每个PSR的最新记录。
这给了我正在寻找的PSR列表:
SELECT Max(Main.PSRMonth) AS MaxOfPSRMonth, Main.PE, Main.Loc, Main.EWO
FROM Main
GROUP BY Main.PE, Main.Loc, Main.EWO
ORDER BY Main.PE, Main.Loc, Main.EWO;
Table:Main
Primary Key is PSRMonth + PE + EWO + LOC
现在,根据上面的选择,我需要所有字段FROM Main
。
我想用一个SQL语句来做这个。
答案 0 :(得分:1)
根据上面的选择,我需要所有字段FROM Main。
然后试试这个:
SELECT m1.*
FROM Main m1
INNER JOIN
(
SELECT
Max(PSRMonth) AS MaxOfPSRMonth,
PE,
Loc,
EWO
FROM Main
GROUP BY PE, Loc, EWO
) m2 ON m1.PE = m2.PE AND m1.Loc = m2.Loc AND m1.EWO = m2.EWO
AND m1.PSRMonth = m2.MaxOfPSRMonth
ORDER BY m1.PE, M1.Loc, M1.EWO;