Re: Merge overlapping time-periods

Поиск
Список
Период
Сортировка
От F. BROUARD / SQLpro
Тема Re: Merge overlapping time-periods
Дата
Msg-id 4DF8D332.30909@club-internet.fr
обсуждение исходный текст
Ответ на Merge overlapping time-periods  ("Jira, Marcel" <Marcel.Jira@wu.ac.at>)
Список 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 *************************



В списке pgsql-sql по дате отправления:

Предыдущее
От: "Charles N. Charotti"
Дата:
Сообщение: Calling inner functions vs. Begin-End blocs
Следующее
От: "Oliveiros d'Azevedo Cristina"
Дата:
Сообщение: Re: Merge overlapping time-periods