使用范围内的随机数更新每条记录

时间:2017-07-31 01:12:04

标签: sql postgresql postgresql-9.5

我想用从中随机生成的值更新员工表 generate_series(9353500001234,9353500001237)并且还需要确保不应更新重复值。 当我尝试使用generate_series加入employee表时,会产生交叉产品。请建议

CREATE TABLE employee
(
  member_number integer,
  gs1_src_acct_num bigint,
  CONSTRAINT employee_gs1_src_acct_num_key UNIQUE (gs1_src_acct_num)
)  
INSERT INTO lcdm_lnd.employee(member_number) VALUES (7360909);
INSERT INTO lcdm_lnd.employee(member_number) VALUES (3142554);
INSERT INTO lcdm_lnd.employee(member_number) VALUES (1510423);
INSERT INTO lcdm_lnd.employee(member_number) VALUES (9605961);

1 个答案:

答案 0 :(得分:0)

我认为这可以满足您的需求:

update employee
    set gs1_src_acct_num = e2.new_gs1_src_acct_num
    from (select e2.*,
                 (row_number() over (order by random()) - 1 + 9353500001234) as new_gs1_src_acct_num
          from employee e2
         ) e2
     where e2.member_number = e.member_number;