当我执行以下查询时,返回的所有5条记录都有一个等级" 1" ...但是我需要记录最多的记录是" 1"然后" 2"等...
我尝试过以下查询的变体,使用OVER()中的不同列等等......没有运气。
SELECT TOP(5)
SearchTerm,
Category,
count(''),
RANK() OVER(PARTITION BY SearchTerm ORDER BY count('') DESC)
FROM
Searches
GROUP BY SearchTerm, Category
ORDER BY count('') desc
答案 0 :(得分:0)
试试这个(可以删除SOMERANK的行)
select ROW_NUMBER() OVER ( ORDER BY SearchTerm) AS MYRANK, *
FROM
(
SELECT TOP(5)
SearchTerm,
Category,
count('') AS MYCOUNT,
RANK() OVER(PARTITION BY SearchTerm ORDER BY count('') DESC) AS SOMERANK
FROM
Searches
GROUP BY SearchTerm, Category
ORDER BY count('') desc
) T
答案 1 :(得分:0)
最简单的解决方案是感谢@Coder of Code(评论原帖)
只需删除不必要的" Partition By"部分。
SELECT TOP(5)
SearchTerm,
Category,
count(''),
RANK() OVER(ORDER BY count('') DESC)
FROM
Searches
GROUP BY SearchTerm, Category
ORDER BY count('') desc