Retrieving value of column X days later

From: Tim Smith <randomdev4+postgres(at)gmail(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Retrieving value of column X days later
Date: 2016-08-07 19:23:07
Message-ID: CA+HuS5EPxEr1jrXnsE2Oe8cBEN-MTpGMe8jM8s=ghaYNZy8u2A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

Let's say I've got a table :

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);

What I would like to do is the following :

(1) Given "select alice,bob from test where foo=1 and bar=2" I would
like to return the values of alice, bob *and* the value of foo four
days later (i.e. "17" in the above example).

(2) But there may be times where there are insufficient data points,
and so I would want to retrieve the last available value (e.g. "select
alice,bob from test where foo=9 and bar=10", there is 4 days hence,
therefore it would return the last available, i.e. "17" in this
example, even though that is only 2 days hence).

Any ideas welcome !

Thanks !

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Sándor Daku 2016-08-07 20:05:48 Re: Retrieving value of column X days later
Previous Message Adrian Klaver 2016-08-07 19:21:54 Re: Should a DB vacuum use up a lot of space ?