Re: ERD for time-based relationships -
08-10-2003
, 03:33 PM
I usually use a history table, something like this:
CREATE TABLE Foobar
(foo_id INTEGER NOT NULL,
start_date DATE NOT NULL,
end_date DATE, -- null means current
CHECK (start_date < end_date),
...
PRIMARY KEY (foo_id, start_date));
and a calendar table with all the enterprise temporal data in it --
holidays, fiscal calendar, etc.
CREATE TABLE Calendar
(cal_date DATE NOT NULL PRIMARY KEY,
...);
And then write queries like this:
SELECT ...
FROM Foobar AS F1, Calendar AS C1
WHERE C1.cal_date BETWEEN F1.start_date
AND COALESCE (F1.end_date, CURRENT_TIMESTAMP)
AND ...;
The COALSECE() makes the data current and I can also pick ranges of
calendar dates. |