全
我一直在尝试这样做,数据遵循结构:
╔══════════════╦═══════════╦══╗
║ Alphabetical ║ Numerical ║ ║
╠══════════════╬═══════════╬══╣
║ A ║ 15 ║ ║
║ A ║ 30 ║ ║
║ E ║ 100 ║ ║
║ C ║ 45 ║ ║
║ F ║ 25 ║ ║
║ C ║ 65 ║ ║
║ B ║ 25 ║ ║
║ F ║ 35 ║ ║
║ C ║ 100 ║ ║
║ A ║ 10 ║ ║
║ C ║ 20 ║ ║
║ B ║ 5 ║ ║
║ E ║ 10 ║ ║
║ F ║ 85 ║ ║
║ D ║ 30 ║ ║
║ F ║ 1 ║ ║
╚══════════════╩═══════════╩══╝要获取以下内容:
╔══════════════╦══════╦═════════╗
║ Alphabetical ║ Rank ║ Numeric ║
╠══════════════╬══════╬═════════╣
║ A ║ 1 ║ 30 ║
║ A ║ 2 ║ 15 ║
║ A ║ 3 ║ 10 ║
║ B ║ 1 ║ 25 ║
║ B ║ 2 ║ 5 ║
║ C ║ 1 ║ 100 ║
║ C ║ 2 ║ 65 ║
║ C ║ 3 ║ 45 ║
║ C ║ 4 ║ 20 ║
║ D ║ 1 ║ 30 ║
║ E ║ 1 ║ 100 ║
║ E ║ 2 ║ 10 ║
║ F ║ 1 ║ 85 ║
║ F ║ 2 ║ 35 ║
║ F ║ 3 ║ 25 ║
║ F ║ 4 ║ 1 ║
╚══════════════╩══════╩═════════╝基本上,将字母字段按升序排序,数值字段按降序排序,并使用按字母字段分组的数值字段的顺序来获得顺序或排名。
只有当我将其限制为字母列中的一个特定值时,我才能做到这一点,方法如下:
select ordered_src.*, ROWNUM Rank from (select src.* from Source src where alphabetical = 'A' order by Numeric desc) ordered_src;但是我不知道如何得到上面所示的结果。有什么想法吗?另外,有没有在mysql/mssql/等中也可以使用的替代方案?
谢谢!
发布于 2020-05-13 08:58:13
使用row_number()
select s.*,
row_number() over (partition by alphabetical order by numerical desc) as rank
from source s
order by alphabetical, rank;https://stackoverflow.com/questions/61764559
复制相似问题