日期:2014-05-17  浏览次数:20660 次

需要一个SQL排名
通过下列SQL取得了一组数据
select count(*) as count, fileName as name from table where type = 'a01' group by fileName order by count desc

count      name
 25         jk
 23         wtt
 19         f9
 17         wy
 13         jqq
 10         alk



现在我需要加一个排名,期望能得到下面的效果
count      name    rank
 25         jk      1
 23         wtt     2
 19         f9      3
 17         wy      4
 13         jqq     5
 10         alk     6

求教sql的写法。谢谢!

------解决方案--------------------
RANK() 相同排名后也有跳过。
DENSE_RANK() 相同排名不跳过。

select count(*) as count, 
       fileName as name,
       rank over(order by count(*)) as rank
  from table
 where type = 'a01'
 group by fileName
 order by count desc


select count(*) as count, 
       fileName as name,
       dense_rank over(order by count(*)) as dense_rank
  from table
 where type = 'a01'
 group by fileName
 order by count desc

------解决方案--------------------
select count(*) as count, fileName as name,row_number()over(order by count(*) desc ) from table where type = 'a01' group by fileName