sql - 在按三个字段排序的表上添加顺序索引
问题描述
我有一个带有字段 aA、aB、aC 的表 a
我正在尝试使用查询字段 bA、bB、bC、bD 制作表 b:
SELECT a.A, a.B, a.C,
(Sequential integers from first record returned to last record returned) AS D
into b
FROM a WHERE a.C is not null
ORDER BY a.C, a.B DESC, a.A;
表 b 的示例输出:
A, B, C, D
9500, 106.12, 9507, 1
9507, 106.12, 9516, 2
9485, 106.11, 9516, 3
9472, 106.1, 9516, 4
9432, 106.09, 9516, 5
9528, 106.14, 9531, 6
9523, 106.13, 9536, 7
9531, 106.14, 9540, 8
9540, 106.14, 9545, 9
9545, 106.14, 9548, 10
9548, 106.14, 9555, 11
9570, 106.21, 9572, 12
9575, 106.22, 9580, 13
9580, 106.22, 9583, 14
A 是唯一标识符。
这将在具有数百万条记录的表上运行。
我的挑战是(从返回的第一条记录到返回的最后一条记录的顺序升序整数)。有没有人建议在括号中放入什么来帮助我只使用查询来创建表 b?
解决方案
我认为最简单的方法是先定义新表并使用insert
:
create table b (
b_id autoincrement primary key,
a ?, -- ? is for the type of the column
b ?,
c ?
);
insert into b (a, b, c)
select a.A, a.B, a.C,
from a
where a.C is not null
orer by a.C, a.B desc, a.A;
MS Access 没有方便的功能,例如row_number()
(几乎所有其他数据库都支持)来简化此操作。
推荐阅读
- python - 如何在python中对图像进行操作
- swift - URLSession 异步在 Swift 包中不起作用
- web - 我们的前端怎么知道这个http响应属于我们的请求呢?
- java - 在 Android 12 (SDK 31) 中获取 android.app.ForegroundServiceStartNotAllowedException
- oauth-2.0 - 如何在 Postman 中设置多个 Oauth2 范围
- authentication - 如何不在具有相同身份验证服务器的不同 webapps 之间共享身份验证 cookie
- django - 在 django 中验证 url 中的日期的最佳实践?
- python - 如何返回给定 N*N 方阵的对角线或对角线数组
- function - 使用海关间隔研究功能误差
- html - 如何更改输入按钮(方形)的默认外观,使其看起来像三个垂直点?