排名前100的行数

时间:2016-06-15 07:50:24

标签: sql sql-server sql-server-2008-r2 ranking row-number

我选择了:

select col1,col2,col3.....sum(colx) from t1 group by col1,col2,col3...

如何:

1-显示排序最高的前100行(colx1)ASC

2-添加行号

enter image description here

2 个答案:

答案 0 :(得分:2)

使用TOP()ROW_NUMBER()

SELECT TOP 100 t.*,
       ROW_NUMBER() OVER(ORDER BY sum_col DESC) as row_number 
FROM(
    select col1,col2,col3.....
           sum(colx) as sum_col
    from t1 
    group by col1,col2,col3...) t
ORDER BY sum_col 

答案 1 :(得分:1)

使用ROW_NUMBERTOP

SELECT TOP(100)
    col1,
    col2,
    col3,
    SUM(colx),
    Rn = ROW_NUMBER() OVER(ORDER BY SUM(colx) DESC)
FROM t1
GROUP BY
    col1,
    col2,
    col3
ORDER BY Rn