我有一个类似于以下表格:

REQ     PO      LINE    COST    REV
101630  P101630 1       26.48   0
101630  P101630 2       26.82   0
101630  P101630 3       17.83   0
101630  P101630 1       26.48   1
101630  P101630 2       110.00  1
101630  P101630 3       17.83   1
101630  P101630 1       52.96   2
101630  P101630 2       110.00  2
101630  P101630 3       35.66   2


我如何找到想要的结果将是这样的:

REQ     PO      LINE    COST    REV
101630  P101630 1       52.96   2
101630  P101630 2       110.00  2
101630  P101630 3       35.66   2


我尝试使用此方法,但未返回正确结果

select  req, po, line, cost, max(rev)
from table
where po= 'P101630'
group by req, po, line, cost

最佳答案

我会使用(如果您想要每个REQ,PO,LINE的最新修订版):

SELECT  REQ, PO, LINE, COST FROM (
    SELECT REQ, PO, LINE, COST, ROW_NUMBER() OVER (PARTITION REQ, PO, LINE BY ORDER BY REV DESC) AS RN
    FROM TABLE ) A
WHERE RN = 1

关于sql-server-2008 - 寻找最大修订,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43247557/

10-14 10:12