SQL group by获得最新结果

时间:2019-07-05 02:35:51

标签: mysql sql mysql-workbench

我的问题在图片中

SQL查询

Please see the picture

2 个答案:

答案 0 :(得分:0)

从结果中寻找,似乎在进行分组时似乎正在尝试从Rev获取最高值,并且是否存在两个或多个具有最高Rev的记录以获取最大数量。

您可以运行类似的内容

-- Once you have a unique records, select the unique records with largest "Rev"
select
      num
    , week
    , Po_num
    , max(Rev) Rev
from
      (
       -- First get the max "num" for a unique set of week, Po_num and Rev records
       select
             max(num) num
           , week
           , Po_num
           , Rev
       from
             table
       group by
             week
           , Po_num
           , Rev
      )
;  

答案 1 :(得分:0)

如果您需要每个po_num按num和week进行的最后一次转速 您应该对max ver使用子查询

    select * 
    from  table a
    inner join  (

      select    max(rev) max_rev
               , week
               , Po_num
           from  table
           group by week, Po_num
    ) t on t.max_rev  = a.rev 
            and t.po_num  = a.po_num  
            and t.week  = a.week