1

我想对一些数据进行排名

如果我使用了 rank 函数和 tie,则分配了相同的排名,并且每个重复排名的序列中都会出现一个差距。

例子 :

Value | Ranking 
1       1  
1       1 
1       1
1       1 
1       1  
2       6  

编辑:我想知道是否有可能有这两个版本:

Value | Ranking 
1       5    
1       5 
1       5  
1       5  
1       5   
2       6  

Value | Ranking 
1       3  
1       3 
1       3  
1       3 
1       3 
2       6  

我将 1 替换为 3,因为 3 是 1-2-3-4-5 的中值(5 个关系值)

4

4 回答 4

2
SELECT Value, 
count(*) over (partition by value)/2 + rank() over(order by value) as Ranking1, 
count(*) over (partition by value) + rank() over(order by value) -1 as Ranking2 
FROM table
于 2013-08-01T09:18:09.397 回答
1

尝试

select 
    value, 
    RANK() over (order by value) 
             + COUNT(value) OVER (PARTITION BY value)  / 2,
    RANK() over (order by value) 
             + COUNT(value) OVER (PARTITION BY value) - 1
from yourtable t

如果您使用的是 SQL 2005,请使用

(select COUNT(*) from yourtable where value = t.value)

而不是count over子句。

于 2013-08-01T09:17:42.617 回答
0
DECLARE @foo TABLE (VALUE int);
INSERT @foo VALUES (1),(1),(1),(1),(1),(6);

WITH RowRank AS
(
    SELECT
        VALUE,  
        ROW_NUMBER() OVER (PARTITION BY VALUE ORDER BY VALUE) AS rn,
        RANK() OVER (ORDER BY VALUE) AS rk
    FROM
        @foo
), AvgMax AS
(
    SELECT
        VALUE, rn, rk,
        AVG(rn) OVER (PARTITION BY VALUE ORDER BY VALUE) AS av,
        MAX(rn) OVER (PARTITION BY VALUE ORDER BY VALUE) AS mx
    FROM 
        RowRank
)
SELECT
    VALUE,
    CASE WHEN av < rk THEN rk ELSE av END,
    CASE WHEN mx < rk THEN rk ELSE mx END
FROM
    AvgMax;
于 2013-08-01T09:22:42.343 回答
0

你可能想要我一直试图不得到的东西。

ID # Rank
A   100 1
B   90  3
C   90  3
D   80  5
E   80  5

SELECT x.*, 
COUNT(*) AS myRank 
FROM myTablename x 
JOIN myTablename y 
ON x.number > y.number 
GROUP BY id 
ORDER BY myRank

ORDER BY x.number 的结果相同

于 2014-04-21T14:28:30.537 回答