Generate a series of single days from a table of intervals.

From: Paul Linehan <linehanp(at)tcd(dot)ie>
To:
Cc: "pgsql-novice(at)postgresql(dot)org" <pgsql-novice(at)postgresql(dot)org>
Subject: Generate a series of single days from a table of intervals.
Date: 2016-07-27 16:57:58
Message-ID: CAF4RT5T+P_RkdFs230JOgr3Cg==h0tDYS4DAo22DCdTFPQRQ6g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hi all,

I have a table of date intervals (see below for DDL and DML) with a
status of 1 for those periods. Dates outside of these intervals
need a status of 0.

I wish to generate a series of dates from these intervals and
also to generate the intervening dates with a status of 0 (say
in this example for the month of April - the outying date parameters
should be arbitrary - maybe 1 month, maybe three.

So, take the example first record (of the three sample records) below,

The output I need is:

date_val status

2016-04-01 0
2016-04-02 1
2016-04-03 1
2016-04-04 1
2016-04-05 1
2016-04-06 0
2016-04-02 0
..
..<status as appropriate for the rest of the month>
..
2016-04-30 0

I would like to do this using a recursive CTE if it's the best solution,
but I'm open to other approaches - different approaches appreciated.

Please let me know if you require any further information.

TIA and rgs,

Paul...

CREATE TABLE testdate (datein date, dateout date, status int);

INSERT INTO testdate VALUES ('2016-04-02', '2016-04-05', 1);
INSERT INTO testdate VALUES ('2016-04-15', '2016-04-18', 1);
INSERT INTO testdate VALUES ('2016-04-25', '2016-04-30', 1);

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message David G. Johnston 2016-07-27 17:13:38 Re: Generate a series of single days from a table of intervals.
Previous Message KARIN SUSANNE HILBERT 2016-07-21 17:24:52 Re: PostgreSQL v9.4, ERROR: must be owner of database test_db