Re: Retrieving value of column X days later

From: Tim Smith <randomdev4+postgres(at)gmail(dot)com>
To: Victor Yegorov <vyegorov(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Retrieving value of column X days later
Date: 2016-08-07 21:38:17
Message-ID: CA+HuS5EhHnDmaxcFTGj3=sKXPDj3OGcAquqvBUkgiHfrBJkV3w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Thank you Victor Will experiment with this over the next couple of days.

On 7 August 2016 at 21:41, Victor Yegorov <vyegorov(at)gmail(dot)com> wrote:
> 2016-08-07 22:23 GMT+03:00 Tim Smith <randomdev4+postgres(at)gmail(dot)com>:
>>
>> create table test (
>> when date,
>> foo numeric,
>> bar numeric,
>> alice numeric,
>> bob numeric);
>>
>> insert into test values ('2016-01-01',1,2,3,4);
>> insert into test values ('2016-01-02',5,6,7,8);
>> insert into test values ('2016-01-03',9,10,11,12);
>> insert into test values ('2016-01-04',13,14,15,16);
>> insert into test values ('2016-01-05',17,18,19,20);
>
>
> I had to rename column "when" into "when_d", as I do not like quoting
> identifiers.
>
> Try this query with window functions:
>
> SELECT *,lead(foo,4) OVER (ORDER BY when_d),
> last_value(foo) OVER (ORDER BY when_d RANGE BETWEEN UNBOUNDED
> PRECEDING AND UNBOUNDED FOLLOWING)
> FROM test;
>
> This will give you the ability to lookup needed values.
> You'll have to use subquery though, as window functions are evaluated after
> the `WHERE` clause.
>
>
> --
> Victor Y. Yegorov

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Philippe Girolami 2016-08-07 21:55:54 Re: Should a DB vacuum use up a lot of space ?
Previous Message Tim Smith 2016-08-07 21:38:05 Re: Retrieving value of column X days later