Re: recursive sql

From: ml(at)ft-c(dot)de
To: pgsql-sql(at)lists(dot)postgresql(dot)org
Subject: Re: recursive sql
Date: 2020-08-09 15:04:27
Message-ID: f3d074ba-3bba-3772-0775-55600405e847@ft-c.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hello,

It works - the result ist correct
Thank you,

Franz

On 8/9/20 4:22 PM, Igor Andriychuk wrote:
> I copied over your typo :-), should be:
>
> *with* *recursive* r *as*(
> *select*ts, c, row_id*from*rnk*where*rnk.row_id= 1
> *union*
> *select*rnk.ts, rnk.c*0.33+ r.c**0.67*, rnk.row_id
> *from*
> r
> *join*
> rnk
> *on*
> r.row_id= rnk.row_id- 1
> ),
> rnk*as*(
> *select**, *row_number*() *over*(*order**by*ts) row_id*from*tt
> )
> *select*ts, c*from*r*order**by*ts;
>
>> On Aug 8, 2020, at 11:28 PM, ml(at)ft-c(dot)de <mailto:ml(at)ft-c(dot)de> wrote:
>>
>> 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
>>  -- <code> as c2
>> from tt  -- <- I need tt on this place
>>
>>
>> thank you for help
>> Franz
>>
>>
>

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Mike Martin 2020-08-11 10:47:58 Use multidimensional array as VALUES clause in insert
Previous Message Franz Timmer 2020-08-09 14:54:51 Re: recursive sql