agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedFrom: Samed YILDIRIM <samed@reddoc.net>
To: ml@ft-c.de <ml@ft-c.de>
To: pgsql-sql@lists.postgresql.org <pgsql-sql@lists.postgresql.org>
Subject: Re: recursive sql
Date: Sun, 09 Aug 2020 13:38:22 +0300
Message-ID: <29781596968237@mail.yandex.com.tr> (raw)
In-Reply-To: <eaf4082e-79ac-7a5d-bcf8-63ce66087365@ft-c.de>
References: <eaf4082e-79ac-7a5d-bcf8-63ce66087365@ft-c.de>
<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/...;[2]: <a href="https://www.postgresql.org/docs/12/tutorial-window.html">https://www.postgresql.org/docs/12/t...;[3]: <a href="https://www.postgresql.org/docs/12/queries-with.html">https://www.postgresql.org/docs/12/quer... regards.</div><div>Samed YILDIRIM</div><div> </div><div> </div><div> </div><div>09.08.2020, 09:29, "ml@ft-c.de" <ml@ft-c.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>
view thread (16+ messages) latest in thread
Message-ID: <29781596968237@mail.yandex.com.tr>
Permalink: ../29781596968237@mail.yandex.com.tr/
Also on: postgresql.org/message-id/29781596968237@mail.yandex.com.tr
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: samed@reddoc.net, ml@ft-c.de, pgsql-sql@lists.postgresql.org
Subject: Re: recursive sql
In-Reply-To: <29781596968237@mail.yandex.com.tr>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox