英文:
SQL: Select Count Of Instances Up To Each Current Instance For Each Category In Column Ordered By Date
问题
我有一个包括日期和离散类别列(用户名)的表格。
我想要得到每个用户在表格中出现的次数,但作为表格的一列添加。
结果将指示该用户在日期列中的日期之前已经出现了多少次。
例如:
Id Date Username
------ --------
6age 1/3/23 User1
8s9a 1/3/23 User1
d29j 1/3/23 User2
fj48 1/2/23 User1
39k9 1/2/23 User3
a8j3 1/1/23 User1
0ao2 1/1/23 User1
ajd6 1/1/23 User1
am50 1/1/23 User2
amv8 1/1/23 User3
期望的输出:
Id Date Username Instance
------ -------- --------
6age 1/3/23 User1 6
8s9a 1/3/23 User1 5
d29j 1/3/23 User2 2
fj48 1/2/23 User1 4
39k9 1/2/23 User3 2
a8j3 1/1/23 User1 3
0ao2 1/1/23 User1 2
ajd6 1/1/23 User1 1
am50 1/1/23 User2 1
amv8 1/1/23 User3 1
我正在使用SQLite。数据库有2200万行,所以我试图找到一个优化的方法。不幸的是,ID不是数字。不过,我知道可以通过row_number()来解决这个问题。
英文:
I have a table including dates and a discrete category column (username).
I'd like to get the number of times each user has showed up in the table, but added as a column to the table.
The result would indicate how many times that user has showed up in the table up to the date in the date column.
For example:
Id Date Username
------ --------
6age 1/3/23 User1
8s9a 1/3/23 User1
d29j 1/3/23 User2
fj48 1/2/23 User1
39k9 1/2/23 User3
a8j3 1/1/23 User1
0ao2 1/1/23 User1
ajd6 1/1/23 User1
am50 1/1/23 User2
amv8 1/1/23 User3
Desired output:
Id Date Username Instance
------ -------- --------
6age 1/3/23 User1 6
8s9a 1/3/23 User1 5
d29j 1/3/23 User2 2
fj48 1/2/23 User1 4
39k9 1/2/23 User3 2
a8j3 1/1/23 User1 3
0ao2 1/1/23 User1 2
ajd6 1/1/23 User1 1
am50 1/1/23 User2 1
amv8 1/1/23 User3 1
I am working with SQLite. The database has 22 million rows, so I am trying to find an optimized approach. Sadly, the ID is not numeric. Though, I know I can overcome that with row_number().
答案1
得分: 2
一个可行的解决方案包括两个步骤:
- 使用新字段更新表的模式
- 在匹配的唯一标识符上进行更新,同时使用计算的行号(根据用户分区,按日期和标识符排序)
ALTER TABLE tab ADD COLUMN Instance INT;
UPDATE tab
SET Instance = cte.rn
FROM (SELECT id,
ROW_NUMBER() OVER(PARTITION BY Username ORDER BY Date, id DESC) AS rn
FROM tab) cte
WHERE tab.id = cte.id;
输出:
Id | Date | Username | Instance |
---|---|---|---|
6age | 1/3/23 | User1 | 6 |
8s9a | 1/3/23 | User1 | 5 |
d29j | 1/3/23 | User2 | 2 |
fj48 | 1/2/23 | User1 | 4 |
39k9 | 1/2/23 | User3 | 2 |
a8j3 | 1/1/23 | User1 | 2 |
0ao2 | 1/1/23 | User1 | 3 |
ajd6 | 1/1/23 | User1 | 1 |
am50 | 1/1/23 | User2 | 1 |
amv8 | 1/1/23 | User3 | 1 |
在此处查看演示:链接。
英文:
A working solution would feature two steps:
- update the schema of your table with the new field
- update matching unique ids, while using a computed row-number (partitioning on users, ordering on dates and ids)
ALTER TABLE tab ADD COLUMN Instance INT;
UPDATE tab
SET Instance = cte.rn
FROM (SELECT id,
ROW_NUMBER() OVER(PARTITION BY Username ORDER BY Date, id DESC) AS rn
FROM tab) cte
WHERE tab.id = cte.id;
Output:
Id | Date | Username | Instance |
---|---|---|---|
6age | 1/3/23 | User1 | 6 |
8s9a | 1/3/23 | User1 | 5 |
d29j | 1/3/23 | User2 | 2 |
fj48 | 1/2/23 | User1 | 4 |
39k9 | 1/2/23 | User3 | 2 |
a8j3 | 1/1/23 | User1 | 2 |
0ao2 | 1/1/23 | User1 | 3 |
ajd6 | 1/1/23 | User1 | 1 |
am50 | 1/1/23 | User2 | 1 |
amv8 | 1/1/23 | User3 | 1 |
Check the demo here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论