【问题标题】:An example of how to grab the last Row_number() in sql?如何在 sql 中获取最后一个 Row_number() 的示例?
【发布时间】:2015-05-04 18:24:37
【问题描述】:

我的表格示例:

我知道,如果我想从每家公司中获得最高分,我会说:

 where columnName = 1

但是如何在不设置 columnName = 4 的情况下抓住最后一个,因为我想要最高和最低,并且每个公司的分数都会不同。

【问题讨论】:

  • 为什么在使用 SQL Server 时标记了这个 MySQL?您使用的是什么版本的 SQL Server?

标签: mysql sql max min


【解决方案1】:

如果您正在寻找最高分和最低分,这里有一个选项:

with cte as (
  select row_number() over (partition by company order by score) minscore,
         row_number() over (partition by company order by score desc) maxscore,
         company, score
  from yourtable
  )
select company, score
from cte 
where minscore = 1 or maxscore = 1

【讨论】:

    【解决方案2】:

    您的初始查询使用 DESC 的顺序将最高的放在第一位.. 只需将其反转: 将其保留为 ASC,顺序将被反转.. 再次选择 =1 项..

    select ROW_NUMBER() OVER (partition by companyname order by weightedscore ASC ) ...
    

    【讨论】:

      【解决方案3】:

      这是给 MySQL 的

       SELECT MAX(WeightedScore),
                MIN(WeightedScore) 
         FROM table
         GROUP BY CompanyName;
      

      对于 SQL Server,请参阅 https://stackoverflow.com/a/1299598/4576237

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-19
        • 2019-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多