From: Oliveiros d'Azevedo Cristina <oliveiros.cristina@marktest.pt>
To: Jira, Marcel <Marcel.Jira@wu.ac.at>
To: pgsql-sql@postgresql.org
Subject: Re: Merge overlapping time-periods
Date: Wed, 15 Jun 2011 17:37:51 +0100
Message-ID: <BFA62FEB05884AEDBF93FD7331BF236E@marktestcr.marktest.pt> (raw)
References: <D793F5C522F1DD40BB9DC43586C57637DE06389FE8@MBX-B.ad.wu-wien.ac.at>
Howdy, Marcel,
In the example output you provided the ID = 2 should have just one record...Ain't I right?
Best,
Oliveiros
----- Original Message -----
From: Jira, Marcel
To: 'pgsql-sql@postgresql.org'
Sent: Wednesday, June 15, 2011 4:23 PM
Subject: [SQL] Merge overlapping time-periods
Hi!
Although I try for some time, I am not able to write an SQL-Query that can do the following:
I have a very big table (let's call it "mytable") with information like this:
ID BEG END
1 2000-01-01 2000-03-31
1 2000-04-01 2000-05-31
1 2000-04-15 2000-07-31
1 2000-09-01 2000-10-31
2 2000-02-01 2000-03-15
2 2000-01-15 2000-03-31
2 2000-04-01 2000-04-15
3 2000-06-01 2000-06-15
3 2000-07-01 2000-07-15
There's an ID and time periods defined by a start value (BEG) and an end value (END)
I want to merge all periods belonging to the same ID, iff their time periods are overlapping or in a direct sequence.
Therefore the result should somehow look like this:
ID BEG END1 2000-01-01 2000-07-311 2000-09-01 2000-10-312 2000-01-15 2000-03-312 2000-04-01 2000-04-153 2000-06-01 2000-06-153 2000-07-01 2000-07-15
I tried using "WITH RECURSIVE" but I didn't succeed.
My server is PostgreSQL 8.4. Unfortunately I can't do anything like update or install some fancy module.
Thank you for your help!
Best regards,
Marcel Jira
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: oliveiros.cristina@marktest.pt, Marcel.Jira@wu.ac.at
Subject: Re: Merge overlapping time-periods
In-Reply-To: <BFA62FEB05884AEDBF93FD7331BF236E@marktestcr.marktest.pt>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox