我想对一些数据进行排名
如果我使用带有关系的排名函数,则会分配相同的排名,并且每个重复排名的序列中都会出现差距。
示例:
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个关系值)
答案 0 :(得分: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
答案 1 :(得分: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
子句。
答案 2 :(得分: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;
答案 3 :(得分: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相同的结果