2015-05-04 58 views
1

我的表例如:如何抓取sql中最後一個Row_number()的例子?

enter image description here

我知道,如果我想抓住從每家公司的最高分,我會說:

where columnName = 1 

但我怎麼搶一個是在結束沒有把columnName = 4,因爲我想要最高和最少,每個公司將不同數量的分數。

+2

當你使用SQL Server你爲什麼標記此MySQL的?你使用的是什麼版本的SQL Server? –

回答

0

您最初的查詢中使用ORDER BY DESC帶來最高的第一..只是扭轉: 離開它ASC和秩序將被反轉..再次選擇= 1項..

select ROW_NUMBER() OVER (partition by companyname order by weightedscore ASC) ... 
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