Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 9th, 2004, 05:57 AM
Palsam Palsam is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 13 Palsam User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Crosstable query returns too many records

I'm writing a work schedule application in ASP, and I am using an Access 2000 DB with the following tables:

Code:
WorkSchedule
--------------------------
| ID | Date | EmployeeID |
|  .
|  .
|  .

Employees
---------------------
| EmployeeID | Name |
|  .
|  .
|  .

Status
----------------------------------------------------
| StatusID | DateFrom | DateTo | EmployeeID | Type |
|  .
|  .
|  .


I need to retrieve a list of employees, which are not scheduled for work between the given dates
and which have no status (sick leave, maternity leave etc). The object is to see which employees are available for scheduling on the different dates.

The resulting table/recordset should contain one record per date per available employee (see example below).

Code:
AvailableEmployees
-------------------------
| Date     | EmployeeID |
-------------------------
| 01/01/01 |    2       |
| 01/01/01 |    5       |
| 01/01/01 |    7       |
| 03/01/01 |    2       |
|  .
|  .

We first disregard the statuses. The query below returns a partially correct result. On the dates where only one employee is scheduled, only the
remaining employees are returned (correct). However, on the dates where 2 or more are scheduled, ALL employees are returned (wrong).
Code:
StartDate = "01/01/04" '(mm/dd//yy)
StopDate = "02/30/04"

Query = ""
Query = Query + "SELECT WorkSchedule.Date, Employees.EmployeeID "
Query = Query + "FROM WorkSchedule, Employees "
Query = Query + "WHERE WorkSchedule.Date BETWEEN #StartDate# AND #StopDate# "
Query = Query +    "AND Employees.EmployeeID <> WorkSchedule.EmployeeID "
Query = Query + "GROUP BY WorkSchedule.Date, Employees.EmployeeID"

Including the status condition (see query below) has no affect on the result (I assume the condition is incorrect)
Code:
Query = ""
Query = Query + "SELECT WorkSchedule.Date, Employees.EmployeeID "
Query = Query + "FROM WorkSchedule, Employees "
Query = Query + "WHERE WorkSchedule.Date BETWEEN #StartDate# AND #StopDate# "
Query = Query +    "AND NOT EXISTS (SELECT * FROM Status "
Query = Query +       "WHERE Status.EmployeeID = WorkSchedule.EmployeeID "
Query = Query +       "AND (Status.DateFrom <= WorkSchedule.Date AND Status.DateFrom >= WorkSchedule.Date)) "
Query = Query +    "AND Employees.EmployeeID <> WorkSchedule.EmployeeID "
Query = Query + "GROUP BY WorkSchedule.Date, Employees.EmployeeID"

I guess I am overlooking something trivial, but I can't seem to get the query right. Suggestions, please?

Palsam

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Crosstable query returns too many records


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT