Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k4jxX-0000sw-LW for pgsql-sql@arkaria.postgresql.org; Sun, 09 Aug 2020 11:57:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k4jxW-00087j-9V for pgsql-sql@arkaria.postgresql.org; Sun, 09 Aug 2020 11:57:50 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k4jxW-00087c-2i for pgsql-sql@lists.postgresql.org; Sun, 09 Aug 2020 11:57:50 +0000 Received: from einhorn-mail.in-berlin.de ([217.197.80.20]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k4jxT-0002dl-9E for pgsql-sql@lists.postgresql.org; Sun, 09 Aug 2020 11:57:49 +0000 X-Envelope-From: ml@ft-c.de X-Envelope-To: Received: from authenticated.user (localhost [127.0.0.1]) by einhorn.in-berlin.de with ESMTPSA id 079Bvh7N021775 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT) for ; Sun, 9 Aug 2020 13:57:43 +0200 Reply-To: ml@ft-c.de Subject: Re: recursive sql References: <29781596968237@mail.yandex.com.tr> To: pgsql-sql@lists.postgresql.org From: ml@ft-c.de Message-ID: <65f26434-b2ed-7fc2-a5ad-e0365e657d5a@ft-c.de> Date: Sun, 9 Aug 2020 13:57:43 +0200 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <29781596968237@mail.yandex.com.tr> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: de-DE Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hallo, with the window function lag there is a shift of one or more rows. Every row connects to the previous row := lag(column,1). What I am looking for: ts c c2 .. 1 1 -- or null in the first row .. 2 3 -- it is the result of 1 + 2 .. 3 6 -- it is the result of 3 + 3 .. 4 10 -- it is the result of 6 + 4 Franz On 8/9/20 12:38 PM, Samed YILDIRIM wrote: > Hi Franz, > Simply you can use window functions[1][2]. > pgsql-sql=# select *, lag(c) over (order by ts) as c2 from tt; > ts | c | c2 > ---------------------+---+---- > 2019-12-31 00:00:00 | 1 | > 2020-01-01 00:00:00 | 2 | 1 > 2020-07-02 00:00:00 | 3 | 2 > 2020-07-06 00:00:00 | 4 | 3 > 2020-07-07 00:00:00 | 5 | 4 > 2020-07-08 00:00:00 | 6 | 5 > (6 rows) > I personally prefer to use window functions due to their simplicity. If > you still want to use recursive query: [3] > pgsql-sql=# with recursive rc as ( > select * from (select ts,c,null::numeric as c2 from tt order by ts asc > limit 1) k1 > union > 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 > ) > select * from rc; > ts | c | c2 > ---------------------+---+---- > 2019-12-31 00:00:00 | 1 | > 2020-01-01 00:00:00 | 2 | 1 > 2020-07-02 00:00:00 | 3 | 2 > 2020-07-06 00:00:00 | 4 | 3 > 2020-07-07 00:00:00 | 5 | 4 > 2020-07-08 00:00:00 | 6 | 5 > (6 rows) > [1]: https://www.postgresql.org/docs/12/functions-window.html > [2]: https://www.postgresql.org/docs/12/tutorial-window.html > [3]: https://www.postgresql.org/docs/12/queries-with.html > Best regards. > Samed YILDIRIM > 09.08.2020, 09:29, "ml@ft-c.de" : > > Hello, > > the table > create table tt ( >    ts timestamp, >    c numeric) ; > > insert into tt values >   ('2019-12-31',1), ('2020-01-01',2), >   ('2020-07-02',3), ('2020-07-06',4), >   ('2020-07-07',5), ('2020-07-08',6); > > My question: It is possible to get an >    additional column (named c2) >    with >    ( c from current row ) + ( c2 from the previous row ) as c2 > > the result: > ts c c2 > .. 1 1 -- or null in the first row > .. 2 3 > .. 3 6 > .. 4 10 > ... > > with recursive ema as () > select ts, c, >    -- many many computed_rows >    -- as c2 > from tt -- <- I need tt on this place > > > thank you for help > Franz >