Postgresql 用于更改列的值(var char(20))的SQL
表4:员工详细信息Postgresql 用于更改列的值(var char(20))的SQL,postgresql,postgresql-9.1,Postgresql,Postgresql 9.1,表4:员工详细信息 -------------------------------------------- c_bp_id | value | name |address -------------------------------------------- 10001 |001 |john | 10002 |mathew |mathew | 10003 |002 |Ann | 10004 |003 |isabel | 10005 |mari
--------------------------------------------
c_bp_id | value | name |address
--------------------------------------------
10001 |001 |john |
10002 |mathew |mathew |
10003 |002 |Ann |
10004 |003 |isabel |
10005 |maria |maria |
10006 |Adam |Adam |
我想将中的“值”列更改为
--------------------------------------------
c_bp_id | value | name |address
--------------------------------------------
10001 |001 |john |
10002 |**005**|mathew |
10003 |002 |Ann |
10004 |003 |isabel |
10005 |**006**|maria |
10006 |**007**|Adam |
请帮帮我好吗?可能是这样的:
with cte as (
-- create row_numbers for all values where value is text
select
c_bp_id,
row_number() over(order by c_bp_id) as rn
from Table1
where value !~ E'^\\d+$'
)
-- updated
update Table1 as t set
value = lpad((rn + 1 + (select coalesce(max(value::int), 0) from Table1 where value ~ E'^\\d+$'))::text, 3, '0')
from cte as c
where c.c_bp_id = t.c_bp_id;
这是一个wierd查询,我不建议将其投入生产,但如果是一次性更新,您可以使用它。我也不知道为什么你跳过了
004
值,开始从005
开始计算。你有没有尝试过查看更新?看看这个,然后自己试试看。那里的星号是因为它们是数据的必需部分,还是强调这些是数据更改?