I posted this question last week. Still having problems.
I have a database that stores information on vacation time taken by employees. The two Date/Time data type fields [FirstDayOff] and [ReturningOn] record the vacation days. Days between those two dates are counted vacation days taken by the employee.
I need help writing the criteria for a query that will return the records of all employees who were off between any two date parameters, [Report Start Date] and [Report End Date] entered by the user.
Any help would be much appreciated.
Golom (Programer) responded with the following post.
There are several possibilities for this.
The employee days off may
- begin before, and end after the report interval.
- begin between the report dates but end after the end of the report.
- begin before the report start but end within the report interval.
- begin and end within the report interval.
CODE
Select EmpID
, DateDiff("d", [Report Start Date], [Report End Date]) As Days
From myTable
Where [Report Start Date] >= [FirstDayOff]
AND [Report End Date] <= [ReturningOn]
UNION
Select EmpID
, DateDiff("d", [FirstDayOff], [Report End Date]) As Days
From myTable
Where [Report Start Date] <= [FirstDayOff]
AND [Report End Date] < [ReturningOn]
UNION
Select EmpID
, DateDiff("d", [Report Start Date], [ReturningOn]) As Days
From myTable
Where [Report Start Date] > [FirstDayOff]
AND [Report End Date] >= [ReturningOn]
UNION
Select EmpID
, DateDiff("d", [FirstDayOff], [ReturningOn]) As Days
From myTable
Where [Report Start Date] < [FirstDayOff]
AND [Report End Date] > [ReturningOn]
This was helpful to me in that it provided a count of the number of vacation days for each employee who had taken time off. But this returned all the records and I need to limit the records returned to only those employees who were off during the report interval. If for example the parameters for the report were [Report Start Date] 10/2/2006, [Report End Date] 10/6/2006.
It would return records like the following:
EmpID FirstDayOff ReturningOn
1 9/30/2006 10/4/2006
(Off on the 2nd, 3rd)
2 10/2/2006 10/7/2006
(Off on the 2nd, 3rd, 4th, 5th)
3 10/3/2006 10/5/2006
(Off on the 4th)
But it would exclude records like the following:
4 9/30/2006 10/1/2006
(Dates are before report interval)
5 10/7/2006 10/12/2006
(Dates are after report interval)
I have a database that stores information on vacation time taken by employees. The two Date/Time data type fields [FirstDayOff] and [ReturningOn] record the vacation days. Days between those two dates are counted vacation days taken by the employee.
I need help writing the criteria for a query that will return the records of all employees who were off between any two date parameters, [Report Start Date] and [Report End Date] entered by the user.
Any help would be much appreciated.
Golom (Programer) responded with the following post.
There are several possibilities for this.
The employee days off may
- begin before, and end after the report interval.
- begin between the report dates but end after the end of the report.
- begin before the report start but end within the report interval.
- begin and end within the report interval.
CODE
Select EmpID
, DateDiff("d", [Report Start Date], [Report End Date]) As Days
From myTable
Where [Report Start Date] >= [FirstDayOff]
AND [Report End Date] <= [ReturningOn]
UNION
Select EmpID
, DateDiff("d", [FirstDayOff], [Report End Date]) As Days
From myTable
Where [Report Start Date] <= [FirstDayOff]
AND [Report End Date] < [ReturningOn]
UNION
Select EmpID
, DateDiff("d", [Report Start Date], [ReturningOn]) As Days
From myTable
Where [Report Start Date] > [FirstDayOff]
AND [Report End Date] >= [ReturningOn]
UNION
Select EmpID
, DateDiff("d", [FirstDayOff], [ReturningOn]) As Days
From myTable
Where [Report Start Date] < [FirstDayOff]
AND [Report End Date] > [ReturningOn]
This was helpful to me in that it provided a count of the number of vacation days for each employee who had taken time off. But this returned all the records and I need to limit the records returned to only those employees who were off during the report interval. If for example the parameters for the report were [Report Start Date] 10/2/2006, [Report End Date] 10/6/2006.
It would return records like the following:
EmpID FirstDayOff ReturningOn
1 9/30/2006 10/4/2006
(Off on the 2nd, 3rd)
2 10/2/2006 10/7/2006
(Off on the 2nd, 3rd, 4th, 5th)
3 10/3/2006 10/5/2006
(Off on the 4th)
But it would exclude records like the following:
4 9/30/2006 10/1/2006
(Dates are before report interval)
5 10/7/2006 10/12/2006
(Dates are after report interval)