pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedMerge overlapping time-periods
4+ messages / 3 participants
[nested] [flat]
* Merge overlapping time-periods
@ 2011-06-15 15:23 Jira, Marcel <Marcel.Jira@wu.ac.at>
0 siblings, 2 replies; 4+ messages in thread
From: Jira, Marcel @ 2011-06-15 15:23 UTC (permalink / raw)
To: pgsql-sql
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 END
1 2000-01-01 2000-07-31
1 2000-09-01 2000-10-31
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
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
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Merge overlapping time-periods
@ 2011-06-15 15:43 F. BROUARD / SQLpro <sqlpro@club-internet.fr>
parent: Jira, Marcel <Marcel.Jira@wu.ac.at>
1 sibling, 0 replies; 4+ messages in thread
From: F. BROUARD / SQLpro @ 2011-06-15 15:43 UTC (permalink / raw)
To: pgsql-sql
I write a paper on this topic comparing queries for PG, SQL Server and
MySQL.
Can you read french ?
http://blog.developpez.com/sqlpro/p9821/langage-sql-norme/agregation-d-intervalles-en-sql-1/
The worst query is the RECURSIVE one !
A +
Le 15/06/2011 17:23, Jira, Marcel a écrit :
> 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 END
>
> 1 2000-01-01 2000-07-31
>
> 1 2000-09-01 2000-10-31
>
> 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
>
> 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
>
--
Frédéric BROUARD - expert SGBDR et SQL - MVP SQL Server - 06 11 86 40 66
Le site sur le langage SQL et les SGBDR : http://sqlpro.developpez.com
Enseignant Arts & Métiers PACA, ISEN Toulon et CESI/EXIA Aix en Provence
Audit, conseil, expertise, formation, modélisation, tuning, optimisation
*********************** http://www.sqlspot.com *************************
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Merge overlapping time-periods
@ 2011-06-15 16:37 Oliveiros d'Azevedo Cristina <oliveiros.cristina@marktest.pt>
parent: Jira, Marcel <Marcel.Jira@wu.ac.at>
1 sibling, 1 reply; 4+ messages in thread
From: Oliveiros d'Azevedo Cristina @ 2011-06-15 16:37 UTC (permalink / raw)
To: Jira, Marcel <Marcel.Jira@wu.ac.at>; pgsql-sql
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
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Merge overlapping time-periods
@ 2011-06-15 16:49 Oliveiros d'Azevedo Cristina <oliveiros.cristina@marktest.pt>
parent: Oliveiros d'Azevedo Cristina <oliveiros.cristina@marktest.pt>
0 siblings, 0 replies; 4+ messages in thread
From: Oliveiros d'Azevedo Cristina @ 2011-06-15 16:49 UTC (permalink / raw)
To: Oliveiros d'Azevedo Cristina <oliveiros.cristina@marktest.pt>; Jira, Marcel <Marcel.Jira@wu.ac.at>; pgsql-sql
Hello again, Marcel.
I tried this and it seems to work on the example you provided, iif my understanding is correct and you want the ID=2 to have just one record on final output.
That makes sense to me because
2 2000-01-15 2000-03-31
2 2000-04-01 2000-04-15
are in direct sequence (IMHO) as much as
1 2000-01-01 2000-03-31
1 2000-04-01 2000-05-31
are. Isn't my understanding correct?
Best,
Oliveiros
(SELECT x."ID",x."BEG",x."END"
FROM mytable x
LEFT JOIN
(
SELECT a."ID" as xid ,a."BEG" as xbeg,a."END" as xend,b."ID" as yid,b."BEG" as ybeg,b."END" as yend
FROM mytable a
JOIN mytable b
ON a."ID" = b."ID"
AND (( ((a."BEG",a."END") OVERLAPS (b."BEG",b."END"))
OR ((b."BEG" - a."END") = 1))
AND (a."BEG" <> b."BEG")
AND (b."END" <> a."END"))
) y
ON (((yid = x."ID")
AND (ybeg = x."BEG")
AND (yend = x."END"))
OR ((xid = x."ID")
AND (xbeg = x."BEG")
AND (xend = x."END")))
WHERE yid IS NULL)
UNION (
SELECT x."ID",MIN(x."BEG"),MAX(x."END")
FROM mytable x
LEFT JOIN
(
SELECT a."ID" as xid,a."BEG" as xbeg,a."END" as xend,b."ID" as yid,b."BEG" as ybeg,b."END" as yend
FROM mytable a
JOIN mytable b
ON a."ID" = b."ID"
AND (( ((a."BEG",a."END") OVERLAPS (b."BEG",b."END"))
OR ((a."BEG" - b."END") = 1))
AND (a."BEG" <> b."BEG")
AND (b."END" <> a."END"))
) y
ON (((yid = x."ID")
AND (ybeg = x."BEG")
AND (yend = x."END"))
OR ((xid = x."ID")
AND (xbeg = x."BEG")
AND (xend = x."END")))
WHERE yid IS NOT NULL
GROUP BY x."ID"
)
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
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2011-06-15 16:49 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-06-15 15:23 Merge overlapping time-periods Jira, Marcel <Marcel.Jira@wu.ac.at>
2011-06-15 15:43 ` F. BROUARD / SQLpro <sqlpro@club-internet.fr>
2011-06-15 16:37 ` Oliveiros d'Azevedo Cristina <oliveiros.cristina@marktest.pt>
2011-06-15 16:49 ` Oliveiros d'Azevedo Cristina <oliveiros.cristina@marktest.pt>
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