排名函数给出不正确的排名
Rank function gives incorrect Ranking
select distinct a.pattern ,convert(numeric(18,0) ,isnull(b.[DCount]as [DCount]
,DENSE_RANK () OVER ( Partition by a.pattern order by b.[DCount]desc ) as [Rank]
from a, b
输出为
Pattern Dcount Rank
A 0 1
A 0 1
A 0 2
A 0 2
B 0 2
B 0 2
B 0 2
B 0 2
C 0 2
C 0 2
C 0 2
而所需的输出是
Pattern Dcount Rank
A 0 1
A 0 1
A 0 1
A 0 1
B 0 1
B 0 1
B 0 1
B 0 1
C 0 1
C 0 1
C 0 1
使用下面的方式DENSE_RANK
select * ,DENSE_RANK () OVER ( order by Pattern, Dcount) as rn from t
也许您只想按 dcount
排名。如果是:
select distinct a.pattern, convert(numeric(18,0),
coalesce(b.DCount, 0) as DCount,
dense_rank() over (order by b.[DCount] desc) as [Rank]
from a cross join b;
试试这个:您需要按模式分区,dcount 并仅按模式排序
select *,dense_Rank() over(partition by Pattern,dcount order by Pattern) from tablename
select distinct a.pattern ,convert(numeric(18,0) ,isnull(b.[DCount]as [DCount]
,DENSE_RANK () OVER ( Partition by a.pattern order by b.[DCount]desc ) as [Rank]
from a, b
输出为
Pattern Dcount Rank
A 0 1
A 0 1
A 0 2
A 0 2
B 0 2
B 0 2
B 0 2
B 0 2
C 0 2
C 0 2
C 0 2
而所需的输出是
Pattern Dcount Rank
A 0 1
A 0 1
A 0 1
A 0 1
B 0 1
B 0 1
B 0 1
B 0 1
C 0 1
C 0 1
C 0 1
使用下面的方式DENSE_RANK
select * ,DENSE_RANK () OVER ( order by Pattern, Dcount) as rn from t
也许您只想按 dcount
排名。如果是:
select distinct a.pattern, convert(numeric(18,0),
coalesce(b.DCount, 0) as DCount,
dense_rank() over (order by b.[DCount] desc) as [Rank]
from a cross join b;
试试这个:您需要按模式分区,dcount 并仅按模式排序
select *,dense_Rank() over(partition by Pattern,dcount order by Pattern) from tablename