Skip to content
Advertisement

Calculate a column value backwards over a series of previous rows/RECURSIVE/CONNECTED BY

need your help. I guess/hope there is a function for that. I found “CONNECT DBY” and “WITH RECURSIVE AS …” but it doesn’t seem to solve my problem.

GIVEN TABLES:

Table A

Table B

(points always to the lates entry in table “A”. Update, no history)

Join Statement:

SELECT id, prev_id, key, date FROM A LEFT OUTER JOIN B ON B.ref_id = A.id

GIVEN psql result set:

DESIRED output:

The rows of the result set are connected by columns ‘id’ and ‘prev_id’. I want to calculate the “key” column in a reasonable time. Keep in mind, this is a very simplified example. Normally there are a lot of more rows and different keys and id’s

Advertisement

Answer

I understand that you want to bring the hierarchy of each row in tableb. Here is one approach using a recursive query:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement