Re: How to Find Overlapping Date Ranges in a Table
- From: "Charles" <blank@xxxxxxxxxxx>
- Date: Wed, 10 Jun 2009 09:02:43 +0100
Thank you for taking the time to comment on my style of asking a question,
but really, you needn't have troubled yourself. Two other people have
managed to decipher my apparently cryptic question and given succinct and
helpful answers.
Perhaps you have a link to "How to answer questions in a newsgroup"? I
imagine it might talk about grandmother's and egg sucking.
I have read the remainder of your reply with interest. Thank you for that.
Charles
"--CELKO--" <jcelko212@xxxxxxxxxxxxx> wrote in message
news:d4f701e2-a12a-4c9f-a1c7-cdb910fbbea2@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative
Referential Integrity, data types, etc. in your schema are. If you
know how, follow ISO-11179
data element naming conventions and formatting rules. Temporal data
should use ISO-8601
formats. Code should be in Standard SQL as much as possible and not
local dialect.
Sample data is also a good idea, along with clear specifications. It
is very hard to debug
code when you do not let us see it. If you want to learn how to ask a
question on a Newsgroup,
look at: http://www.catb.org/~esr/faqs/smart-questions.html
I have a table with a PK, two FKs and two dates (datetime)[are they DATE
or DATETIME?] : PeriodStart and PeriodEnd. <<
By definition, it has to have a Primary Key to even be a table. Wild
ass guessing lead me to this skeleton.
CREATE TABLE FoobarPeriods
(foobar_id INTEGER NOT NULL PRIMARY KEY,
period_start_date DATE NOT NULL,
period_end_date DATE NOT NULL,
CHECK (period_start_date < period_end_date),
foo_id INTEGER NOT NULL
REFERENCES Foo(foo_id),
bar_id INTEGER NOT NULL
REFERENCES Bar(bar_id),
..);
Due to some fault, the table has ended up with some duplicate date
ranges and some overlapping ranges, which I need to identify. <<
And you then need to add constraints to prevent this from happening
over and over. The first constraint is UNIQUE (period_start_date,
period_end_date). The second is best done with a TRIGGER in SQL
Server; it still does not have all of the SQL-92 features (I can show
you how to use a VIEW WITH CHECK OPTION, if you wish).
a) FK_1 must not have any periods that overlap, but FK_2 canThere are two scenarios, depending on the particular table:
b) neither FK_1 nor FK_2 can have any overlapping periods <<
This makes no sense. What do the two referenced tables look like?
Read how bad your narrative is!
Can anyone think of a neat way to identify those rogue records [sic:
rows are not records]? <<
You should have a Calendar table in your schema. Look for a calendar
date that falls in two or more ranges
SELECT C.cal_date, MIN(foobar_id), MAX(foobar_id), COUNT(foobar_id)
FROM FoobarPeriods AS F, Calendar AS C
WHERE C.cal_date BETWEEN F.period_start_date AND F.period_end_date
GROUP BY C.cal_date
HAVING COUNT(foobar_id) > 1
.
- References:
- How to Find Overlapping Date Ranges in a Table
- From: Charles
- Re: How to Find Overlapping Date Ranges in a Table
- From: --CELKO--
- How to Find Overlapping Date Ranges in a Table
- Prev by Date: Re: Detecting secureables in SQL 2005/2008
- Next by Date: Re: How to Find Overlapping Date Ranges in a Table
- Previous by thread: Re: How to Find Overlapping Date Ranges in a Table
- Next by thread: Re: How to Find Overlapping Date Ranges in a Table
- Index(es):
Relevant Pages
|