V
V
Vitaly2015-10-27 18:33:20
PostgreSQL
Vitaly, 2015-10-27 18:33:20

How to fill a table cell with the data of another cell?

there is a postgers table
2d06c8a89f5d4db29a4dce36b76e927b.png
in the 4th column (where there are asterisks), if the line in column No. 3 is equal to “minus”, write data = column02 of this line - column02 of the next line ( c = ab)
How to do it

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Vitaliy, 2015-10-28
@Scorpiored88

Wrote a script on nodejs, went through a loop and everything is ok

A
Aleksey Ratnikov, 2015-10-27
@mahoho

In SQL Server, this is done using the LEAD () window function, PostgreSQL seems to have this too :

UPDATE table_name SET column4 = CASE
                             when column4 = 'minus' then column2 - lead(column2) OVER (ORDER BY id)
                             else column4
                           END

S
Sergey, 2015-10-27
Protko @Fesor

UPDATE blabla SET column4 = column1 - column2 WHERE column3...

D
Dmitry Kovalsky, 2015-10-27
@dmitryKovalskiy

I would dig in the direction of the Cursors. I can’t sketch out the code right off the bat, but bypassing the cursor involves sequential processing of each line, and there, in theory, you can do what you want.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question