1

I'd like to rank some data

If I used a rank function with ties, the same ranking is assigned and a gap appears in the sequence for each duplicate ranking.

example :

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

EDIT : I'd like to know if it's possible have these two versions :

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  

I replace 1 by 3 because 3 is the median value of 1-2-3-4-5 (5 ties values)

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 に答える