Question

I want to update a column's value in this way

new value  = old value + row_number() * 1000

also for row_number I want to use order by old value

but I didn't find any solution.

sample data

column    
   1
   3
   5

after update query it should be

column
  1001
  2003
  3005
Was it helpful?

Solution

CREATE VOLATILE TABLE test, NO FALLBACK
(MyCol SMALLINT NOT NULL)
PRIMARY INDEX (MyCol)
ON COMMIT PRESERVE ROWS;

INSERT INTO test VALUES (1);
INSERT INTO test VALUES (3);
INSERT INTO test VALUES (5);

SELECT MyCol FROM test;

UPDATE test
  FROM (SELECT MyCol
             , ROW_NUMBER() OVER (ORDER BY MyCol) AS RowNum_
          FROM test) DT1
   SET MyCol = test.MyCol + (RowNum * 1000)
 WHERE test.MyCol DT1.MyCol;

SELECT MyCol FROM TEST;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top