<div>Hi Franz,</div><div> </div><div>Simply you can use window functions[1][2].</div><div> </div><div><div>pgsql-sql=# select *, lag(c) over (order by ts) as c2 from tt;</div><div>ts | c | c2</div><div>---------------------+---+----</div><div>2019-12-31 00:00:00 | 1 |</div><div>2020-01-01 00:00:00 | 2 | 1</div><div>2020-07-02 00:00:00 | 3 | 2</div><div>2020-07-06 00:00:00 | 4 | 3</div><div>2020-07-07 00:00:00 | 5 | 4</div><div>2020-07-08 00:00:00 | 6 | 5</div><div>(6 rows)</div><div> </div><div> </div><div>I personally prefer to use window functions due to their simplicity. If you still want to use recursive query: [3]</div><div> </div><div><div><div><div>pgsql-sql=# with recursive rc as (</div><div>select * from (select ts,c,null::numeric as c2 from tt order by ts asc limit 1) k1</div><div>union</div><div>select * from (select tt.ts,tt.c,rc.c as c2 from tt, lateral (select * from rc) rc where tt.ts > rc.ts order by tt.ts asc limit 1) k2</div><div>)</div><div>select * from rc;</div><div>ts | c | c2</div><div>---------------------+---+----</div><div>2019-12-31 00:00:00 | 1 |</div><div>2020-01-01 00:00:00 | 2 | 1</div><div>2020-07-02 00:00:00 | 3 | 2</div><div>2020-07-06 00:00:00 | 4 | 3</div><div>2020-07-07 00:00:00 | 5 | 4</div><div>2020-07-08 00:00:00 | 6 | 5</div><div>(6 rows)</div></div></div></div><div> </div><div>[1]: <a href="https://www.postgresql.org/docs/12/functions-window.html">https://www.postgresql.org/docs/12/functions-window.html</a></div><div>[2]: <a href="https://www.postgresql.org/docs/12/tutorial-window.html">https://www.postgresql.org/docs/12/tutorial-window.html</a></div><div>[3]: <a href="https://www.postgresql.org/docs/12/queries-with.html">https://www.postgresql.org/docs/12/queries-with.html</a></div><div> </div></div><div>Best regards.</div><div>Samed YILDIRIM</div><div> </div><div> </div><div> </div><div>09.08.2020, 09:29, "ml(at)ft-c(dot)de" <ml(at)ft-c(dot)de>:</div><blockquote><p>Hello,<br /><br />the table<br />create table tt (<br /> ts timestamp,<br /> c numeric) ;<br /><br />insert into tt values<br /> ('2019-12-31',1), ('2020-01-01',2),<br /> ('2020-07-02',3), ('2020-07-06',4),<br /> ('2020-07-07',5), ('2020-07-08',6);<br /><br />My question: It is possible to get an<br /> additional column (named c2)<br /> with<br /> ( c from current row ) + ( c2 from the previous row ) as c2<br /><br />the result:<br />ts c c2<br />.. 1 1 -- or null in the first row<br />.. 2 3<br />.. 3 6<br />.. 4 10<br />...<br /><br />with recursive ema as ()<br />select ts, c,<br /> -- many many computed_rows<br /> -- <code> as c2<br />from tt -- <- I need tt on this place<br /><br /><br />thank you for help<br />Franz<br /><br /> </p></blockquote>