我有一个像这样的表格设置
ID JOBID REPORTNAME REVISION PDFLOCATION
1 1 RPT1 1 /var/rpt1.pdf
1 2 RPT2 1 /var/rpt2.pdf
1 1 RPT1 2 /var/rpt3.pdf
如何选择所有行,但我只需要每jobid
的最高版本
并且查询的示例结果应为
1 2 RPT2 1 /var/rpt2.pdf
1 1 RPT1 2 /var/rpt3.pdf
答案 0 :(得分:2)
如果我理解正确,您希望为每个ID
/ JOBID
群组选择具有最大修订号的记录。
SELECT t1.*
FROM yourTable t1
INNER JOIN
(
SELECT ID, JOBID, MAX(REVISION) AS REVISION
FROM yourTable
GROUP BY ID, JOBID
) t2
ON t1.ID = t2.ID AND
t1.JOBID = t2.JOBID AND
t1.REVISION = t2.REVISION