SQL 服务器 - 根据列显示行的排名

SQL Server - Display rank of row depending on column

我有一个查询 returns:

RowNumber     Name
===================================
1             Invoice-02116 (1).pdf
1             Invoice-02116 (2).pdf
1             Invoice-02116.pdf
2             Invoice-02116.pdf
3             Invoice-02116.pdf
4             Invoice-02116.pdf
5             Invoice-02116.pdf

我需要的是:

RowNumber     Name
===================================
1             Invoice-02116 (1).pdf
2             Invoice-02116 (2).pdf
3             Invoice-02116.pdf
3             Invoice-02116.pdf
3             Invoice-02116.pdf
3             Invoice-02116.pdf
3             Invoice-02116.pdf

更准确地说,根据名称列显示排名。

我在 RowNumber 列中使用的是:

SELECT ROW_NUMBER() OVER (PARTITION BY ff.Name ORDER BY ff.Name) AS RowNumber, ff.Name

改用DENSE_RANK

SELECT DENSE_RANK() OVER (ORDER BY ff.Name) AS RowNumber... 

以适当的顺序使用 DENSE_RANK

SELECT
    DENSE_RANK() OVER (ORDER BY LEN(Name) DESC, Name) AS RowNumber, 
    Name
FROM yourTable;

Demo