sql - 在SQLite中对相同数据进行排名

标签 sql sqlite

我有下表

CREATE TABLE test(
   ID INT
);


有后续数据

Sample Data

我需要以下结果

ID    Rank
100   1
100   2
105   1
105   2
105   3


我只需要使用普通的SQL查询就没有特殊功能

最佳答案

您可以在相关查询中使用rowid以获得结果。由于rowid唯一地标识为行,因此我们可以在这种情况下使用它进行相关计数以分配行号。

试试这个:

select id, (
        select count(*)
        from test t2
        where t.id = t2.id
            and t2.rowid <= t.rowid
        ) rank
from test t;

关于sql - 在SQLite中对相同数据进行排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42143751/

相关文章:

python - 在没有转储文件的情况下使用 Python 将 SQLite3 数据库表迁移到 MySQL

sql - 在一个语句中选择并分配给变量?

c# - 如何使用 .NET 中的 OleDb 命名空间连接到 Access 数据库?

php - 如何通过合并两个表返回多行

MySQL 数据透视表

android - 执行SQL : is bindargs better?

sql - 在单引号中使用 'like'时的Postgres语法错误

javascript - JS - 将数组或字符串转换为参数列表 [SQLite insert with bind parameters]

ios - sqlite优化读取性能

android - 我收到致命异常 : table cars has no column named year