How do I re-order data in the same table?

心不动则不痛 提交于 2021-01-29 05:47:44

问题


AS-IS:

ID       Version 
5587138  1
6460704  2
6537612  3
6264608  4

TO-BE:

ID       Version
5587138  1
6264608  2
6460704  3
6537612  4

I have to re-order the IDs to match the version order. The data is coming from the same table. I am currently trying to use PL/SQL. I really need help on this issue. Thank you.


回答1:


You can use the merge statement as follows:

merge into your_table trg
using (select id, row_number() over (order by id) as version from your_table) src
on (trg.id = src.id)
when matched then
update set trg.version = src.version;



回答2:


My advice would be don't change the version numbers since if the same version is there for more than one id but use the version no required based on rownum

 SELECT ID,Coldata,rownum version
 FROM ( SELECT ID,Coldata, version FROM TABLE ORDER BY ID,Coldata);


来源:https://stackoverflow.com/questions/60993358/how-do-i-re-order-data-in-the-same-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!