Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2002-05-31 : 15:42:52
|
| I have two tables one that contains Employee Info and the other contains an employee availability date.As time goes by I want to know how many total employees are availableThe query below gives me employees that become available for the week.SELECT COUNT(Employees.EmpID) AS EmployeesAvailable, DATEADD(day, - (1 * DATEPART(dw, EmployeeAvailability.DateAvailable)), EmployeeAvailability.DateAvailable) AS WeekFROM Employees INNER JOIN EmployeeAvailability ON Employees.EmpID = EmployeeAvailability.EmpIDGROUP BY DATEADD(day, - (1 * DATEPART(dw, EmployeeAvailability.DateAvailable)), EmployeeAvailability.DateAvailable)I get the followingEmployeesAvailable Week 2 12/29/2001 1 1/12/2002 1 1/19/2002 1 1/26/2002 1 2/2/2002I need to have it look likeEmployeesAvailable Week 2 12/29/2001 3 1/12/2002 4 1/19/2002 5 1/26/2002 6 2/2/2002Tried ROLL UP AND CUBE But can't seem to get it right.Thanks for you help. |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2002-05-31 : 15:52:18
|
| at first glance it looks to me like you just need to schedule some more employees ....can you post the ddl and some sample dataEDIT . . . do you maybe need to count(EmployeeAvailability.EmpID) rather than Employees.EmpID<O>Edited by - Page47 on 05/31/2002 15:57:05 |
 |
|
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2002-05-31 : 16:04:30
|
| EmployeesEmpID EmpName36860 **** 38324 **** 29756 **** 19482 **** 26139 **** 7596 **** EmployeeAvailabilityEmpID DateAvailable 36860 1/1/2002 38324 1/4/2002 29756 1/15/2002 19482 1/22/2002 26139 1/29/2002 7596 2/5/2002 Sample Data that's all.For right now an employee is either available or notwe're not doing measuring periods. There is only oneentry for each employee. Counting either Employees.EmpIDor EmployeeAvailability.EmpID gives same result. |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2002-05-31 : 16:18:33
|
| AHHHH, your query returns the number of employees who begin their availability that week . . . you you need is a running total . . .<O> |
 |
|
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2002-05-31 : 16:23:33
|
| exactly,I triedSELECT EmployeesAvailable, WeekAvailableFROM (SELECT COUNT(Employees.EmpID) AS EmployeesAvailable, DATEADD(day, - (1 * DATEPART(dw, EmployeeAvailability.DateAvailable)), EmployeeAvailability.DateAvailable) AS WeekAvailable FROM Employees INNER JOIN EmployeeAvailability ON Employees.EmpID = EmployeeAvailability.EmpID GROUP BY DATEADD(day, - (1 * DATEPART(dw, EmployeeAvailability.DateAvailable)), EmployeeAvailability.DateAvailable)) AORDER BY WeekAvailableCOMPUTE SUM(EmployeesAvailable) BY WeekAvailableBut I only getEmployeesAvailable WeekAvailable2 12/29/2001I don't know why I don't get the rest of the results |
 |
|
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2002-05-31 : 16:35:53
|
| Thank you page47 I adapted the Guru's Solution (3)and it works great. |
 |
|
|
|
|
|
|
|