sql - SQL - Ordering by a number which recycles when it reaches 999
问题描述
I'm trying to order by a number which recycles when it reaches 999. The number range runs 1 - 999 and it should always run in ascending order. Currently my SQL (ORDER BY BoxID ASC, Seqno ASC
) is generating the following result:
The box number will always increment by 1 every 9 sequence numbers. so in the example shown in the box 35897 the range should run 999, 1, 2, 3, 4, 5, 6, 7, 8
There are no time stamps which can be used to find the order as the sequential numbers are inserted into the database in a random order.
解决方案
This is a bit of a stab in the dark, but perhaps...
SELECT Box,
Number
FROM YourTable
ORDER BY Box,
ROW_NUMBER() OVER (PARTITION BY Number ORDER BY Box ASC),
Number;
This, however, won't work if a single box has over 999 values for Number
.
DB<>Fiddle showing solution (As DB Fiddle limits the result set to 10 rows, I have had to use a CTE to show relevant rows).
推荐阅读
- apache-spark - spark在单线程和单分区下是否具有广泛的依赖性?
- java - 由于 CORS,无法向 springboot 应用程序发出请求
- python - 使用 Python 遍历辅助 Outlook 收件箱中的文件夹
- python - Lib 通过给它匹配分数和非匹配分数来创建 ROC 曲线和 DET 曲线?
- ios - iOS SwiftUI 表单 onTapGesture 阻止子用户界面功能
- javascript - 重新排列 JSON 对象以在 jspdf-autotable 中显示
- elasticsearch - Elasticsearch“搜索查询词”为 _doc 类型索引中的嵌套对象键返回 0 个提示
- algorithm - 在项目中找不到主要方法
- python - 如何强制用户一次又一次地输入值直到输入OK - 算术计算器
- python - 使用 sklearn 的 Python 中的多元线性回归错误