![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Can I get a little help? Given a very simple table of events, with their durations, I want to consolidate time periods for an event that (1) touch each other or (2) overlap each other. CREATE TABLE Events (event_id VARCHAR(25) NOT NULL, start_date DATE NOT NULL, end_date DATE NOT NULL, CHECK (start_date <= end_date), PRIMARY KEY (event_id, start_date, end_date)); INSERT INTO Events VALUES ('Pie Eating', '2009-01-01', '2009-01-02'), ('Pie Eating', '2009-01-03', '2009-01-05'), ('Pie Eating', '2009-01-04', '2009-01-07'), ('Pie Eating', '2009-02-01', '2009-02-07'); The goal is to reduce these three rows into fewer rows that show how many days we were eating pies. ('Pie Eating', '2009-01-01', '2009-01-07') The first and second rows in the INSERT INTO statement touch each other and can be replaced with: ('Pie Eating', '2009-01-01', '2009-01-05') The third row will overlap with this new row and can be consolidated with it, as shown before. However, the fourth row has data that occurs a month after the first three and it is disjoint from all the other rows. What I want is: 1) A portable, simple cursor solution 2) A Recursive CTE solution I am going to use this in a book, so you will get credit. |
#3
| |||
| |||
|
|
Have you considered the possiblity of storing all the intervals where 'Pie Eating' was NOT taking place? |
|
I haven't worked out the details yet, but this seems to be the outline of a workable solution. |
#4
| |||
| |||
|
|
"--CELKO--" <jcelko212 (AT) earthlink (DOT) net> wrote in message news:1174758437.830959.214400 (AT) y80g2000hsf (DOT) googlegroups.com... Can I get a little help? Given a very simple table of events, with their durations, I want to consolidate time periods for an event that (1) touch each other or (2) overlap each other. CREATE TABLE Events (event_id VARCHAR(25) NOT NULL, start_date DATE NOT NULL, end_date DATE NOT NULL, CHECK (start_date <= end_date), PRIMARY KEY (event_id, start_date, end_date)); INSERT INTO Events VALUES ('Pie Eating', '2009-01-01', '2009-01-02'), ('Pie Eating', '2009-01-03', '2009-01-05'), ('Pie Eating', '2009-01-04', '2009-01-07'), ('Pie Eating', '2009-02-01', '2009-02-07'); The goal is to reduce these three rows into fewer rows that show how many days we were eating pies. ('Pie Eating', '2009-01-01', '2009-01-07') The first and second rows in the INSERT INTO statement touch each other and can be replaced with: ('Pie Eating', '2009-01-01', '2009-01-05') The third row will overlap with this new row and can be consolidated with it, as shown before. However, the fourth row has data that occurs a month after the first three and it is disjoint from all the other rows. What I want is: 1) A portable, simple cursor solution 2) A Recursive CTE solution I am going to use this in a book, so you will get credit. Joe, Have you considered the possiblity of storing all the intervals where 'Pie Eating' was NOT taking place? It seems easy to me, once you have such a set, to make a minimal set of intervals like the ones you specify. The (minimal) number of intervals where Pie Eating was not taking place is within one of the (minimal) number of intervals where Pie Eating was taking place (one or more times). This remains true, even if the past is extended indefinitely into prehistory. You of course do not know for the future whether Pie eating will or will not take place in the future, barring some machine like "Back to the Future". Whenever you insert a new Pie Eating event into the database, you will either shrink one of the non eating intervals by a computable interval, or you will fragment a non eating interval into two disjoint intervals, or you will make no changes at all. I haven't worked out the details yet, but this seems to be the outline of a workable solution. After looking at it for a few minutes, I'm pretty sure that my above answer |
#5
| |||
| |||
|
|
Can I get a little help? Given a very simple table of events, with their durations, I want to consolidate time periods for an event that (1) touch each other or (2) overlap each other. CREATE TABLE Events (event_id VARCHAR(25) NOT NULL, start_date DATE NOT NULL, end_date DATE NOT NULL, CHECK (start_date <= end_date), PRIMARY KEY (event_id, start_date, end_date)); INSERT INTO Events VALUES ('Pie Eating', '2009-01-01', '2009-01-02'), ('Pie Eating', '2009-01-03', '2009-01-05'), ('Pie Eating', '2009-01-04', '2009-01-07'), ('Pie Eating', '2009-02-01', '2009-02-07'); The goal is to reduce these three rows into fewer rows that show how many days we were eating pies. ('Pie Eating', '2009-01-01', '2009-01-07') The first and second rows in the INSERT INTO statement touch each other and can be replaced with: ('Pie Eating', '2009-01-01', '2009-01-05') The third row will overlap with this new row and can be consolidated with it, as shown before. However, the fourth row has data that occurs a month after the first three and it is disjoint from all the other rows. What I want is: 1) A portable, simple cursor solution 2) A Recursive CTE solution I am going to use this in a book, so you will get credit. |
#6
| |||
| |||
|
|
Can I get a little help? Given a very simple table of events, with their durations, I want to consolidate time periods for an event that (1) touch each other or (2) overlap each other. CREATE TABLE Events (event_id VARCHAR(25) NOT NULL, start_date DATE NOT NULL, end_date DATE NOT NULL, CHECK (start_date <= end_date), PRIMARY KEY (event_id, start_date, end_date)); INSERT INTO Events VALUES ('Pie Eating', '2009-01-01', '2009-01-02'), ('Pie Eating', '2009-01-03', '2009-01-05'), ('Pie Eating', '2009-01-04', '2009-01-07'), ('Pie Eating', '2009-02-01', '2009-02-07'); |
#7
| |||
| |||
|
|
Can I get a little help? Given a very simple table of events, with their durations, I want to consolidate time periods for an event that (1) touch each other or (2) overlap each other. CREATE TABLE Events (event_id VARCHAR(25) NOT NULL, start_date DATE NOT NULL, end_date DATE NOT NULL, CHECK (start_date <= end_date), PRIMARY KEY (event_id, start_date, end_date)); INSERT INTO Events VALUES ('Pie Eating', '2009-01-01', '2009-01-02'), ('Pie Eating', '2009-01-03', '2009-01-05'), ('Pie Eating', '2009-01-04', '2009-01-07'), ('Pie Eating', '2009-02-01', '2009-02-07'); The goal is to reduce these three rows into fewer rows that show how many days we were eating pies. ('Pie Eating', '2009-01-01', '2009-01-07') The first and second rows in the INSERT INTO statement touch each other and can be replaced with: ('Pie Eating', '2009-01-01', '2009-01-05') The third row will overlap with this new row and can be consolidated with it, as shown before. However, the fourth row has data that occurs a month after the first three and it is disjoint from all the other rows. What I want is: 1) A portable, simple cursor solution 2) A Recursive CTE solution I am going to use this in a book, so you will get credit. |
#8
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |