如何在sql中获取最后一个Row_number()的示例?

时间:2015-05-04 18:24:37

标签: mysql sql max min

我的表格示例:

enter image description here

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

    let calendar = NSCalendar.currentCalendar()
    let weekday = calendar.component(.CalendarUnitWeekday, fromDate: NSDate())

但如何在不添加columnName = 4的情况下抓住最后一个,因为我想要最高和最少,每个公司将得到不同的分数。

3 个答案:

答案 0 :(得分:2)

如果您正在寻找最高和最低分,请选择以下选项:

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

答案 1 :(得分:0)

这是针对MySQL的

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

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

答案 2 :(得分:0)

您的初始查询使用DESC的订单来获得最高的第一个..只是反过来: 保持ASC并且订单将被撤销..再次选择= 1项..

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