mysql - 查找连续字段,直到字段更改并将序列号添加到具有相同名称的行
问题描述
这建立在这个问题之上:Find contiguous index until certain field changes
基本上有看起来像这样的数据
Name Address Index
----- ------- --
Test 0x0100 0
Test 0x0100 1
Test 0x0100 2
Test 0x0100 3
Test 0x0100 4
Test2 0x0100 5
Test2 0x0100 6
Test2 0x0100 7
Test 0x0100 8
Test 0x0100 9
Test 0x0100 10
Test3 0x0100 11
Test3 0x0100 12
Test 0x0100 13
Test 0x0100 14
Test 0x0100 15
数据按地址排序,然后索引。
然后根据我上一个问题的答案进行如下总结:
Name Address Start End
----- ------- ----- ----
Test 0x0100 0 4
Test2 0x0100 5 7
Test 0x0100 8 10
Test3 0x0100 11 12
Test 0x0100 13 15
我现在想做的是在相同地址空间内具有相同值的那些名称的末尾附加一个序列号,所以是这样的:
Name Address Start End
----- ------- ----- ----
Test_1 0x0100 0 4
Test2 0x0100 5 7
Test_2 0x0100 8 10
Test3 0x0100 11 12
Test_3 0x0100 13 15
这对 MySQL 可行吗?存储过程没问题。
谢谢!
解决方案
这是一个差距和孤岛问题。您可以使用不同的行号来获取组。
但是,如果您已经拥有数据,那么:
select (case when count(*) over (partition by name) = 1 then name
else concat(name, '_', row_number() over (partition by name order by start))
end) as name,
. . .
from (<your query that does the first part here>) t;
窗口函数从 MySQL 8.0 开始可用。
推荐阅读
- javascript - MongooseServerSelectionError: 连接 ECONNREFUSED ::1:27017
- java - 在这个问题中如何避免嵌套的for循环
- java - 如何调用 OAuth 端点以获取在 java 中启用 x509 的 accesstoken?
- python - Python scipy 最小化 - 不产生预期回报的简单示例
- http - 这个访问令牌是如何存储在客户端的,在 FastAPI 的教程“Simple OAuth2 with Password and Bearer”中
- python - 在 Tkinter GUI 中创建内置命令行
- java - 使用 AssertJ 在单元测试中检查 Map 条目内的列表
- woocommerce - Woocommerce- 插入 CROSS 和 UP SELL 产品时仅检索库存产品
- solidity - 无法从水龙头合约中提取 ERC-20 代币
- powershell - 如何获取第 1 行中的分隔符数量并将分隔符数量添加到行尾,然后使用 Powershell 保存为新文件