Re: Help joining several tables
From: Joe Celko (jcelko212_at_earthlink.net)
Date: 05/24/04
- Next message: John Cobb: "Re: Nest stored procedure accessing parent objects"
- Previous message: dw: "Re: Select subquery"
- In reply to: Nils Magnus Englund: "Help joining several tables"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 24 May 2004 08:55:58 -0700
I think your design is wrong. Being absense from work is not an entity;
it is an attribute of a employee on a work day.
CREATE TABLE Attendence
(emp_id INTEGER NOT NULL
REFERENCES Personnel(emp_id)
work_date DATETIME NOT NULL,
date_type CHAR(1) DEFAULT 'W' NOT NULL --w=work
CHECK (date_type IN ('W', 'A', 'H', ..)),
..);
Another thing you might be missing is a Calendar table that holds all of
the temporal data for your company, such as work days and holidays; this
is a standard SQL programming trick.
CREATE TABLE Calendar
(cal_date DATETIME NOT NULL PRIMARY KEY,
date_type CHAR(1) DEFAULT 'W' NOT NULL --w= workday
CHECK (date_type IN ('W', 'H', ..)),
..);
This will give you the status of each employee for each day in the
reporting range (@start_date, @end_date):
SELECT C1.cal_date, A1.emp_id,
COALESCE(A1.date_type, C1.date_type)
FROM Calendar AS C1
LEFT OUTER JOIN
Attendence AS A1
ON C1.cal_date = A1.work_date
WHERE C1.cal_date BETWEEN @start_date AND @end_date;
--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
- Next message: John Cobb: "Re: Nest stored procedure accessing parent objects"
- Previous message: dw: "Re: Select subquery"
- In reply to: Nils Magnus Englund: "Help joining several tables"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|