gpt4 book ai didi

sql - 向postgresql中的列添加递增值

转载 作者:行者123 更新时间:2023-11-29 14:26:02 26 4
gpt4 key购买 nike

我有一个表,其中包含 69000 条记录。现在,如何添加一个新列(比如索引),它应该包含 第一行的值 1,第二行的值 2 ....等等

我想要下面的结果

   id      name      index
--------------------------
9796896 sandy 1
796869 raj 2

最佳答案

添加列并用类似的东西更新它:

with cte as
(
select *
, rank() over (order by id desc) as ranker
from test
)
update test
set index = ranker
from cte
where cte.id = test.id;

关于sql - 向postgresql中的列添加递增值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57819570/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com