gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:28:33 24 4
gpt4 key购买 nike

我有下表

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com