Question

I’m working on showing employees that have not entered in any hours for a previous week. I’m currently working with three tables. One table is a calendar that has the first date of each week. The week format is Sunday to Saturday. The second table is the list of hours entered. The Time table contains the date the time was entered and the employees name. The third table is the list of all the employees. I can’t seem to get the joins to work how I would like them to. The end result I would like to see that Bob entered time in week 7 and 8, but week 9 is null. Thank you for your help. Its greatly appreciated.

Current Code

SELECT
    d.Resource
    ,SUM(p.Hours) AS Hours
    ,m.[WeeksSundayToSaturday]
    ,DatePart(wk, m.[WeeksSundayToSaturday]) AS WeekNumber

FROM CalendarWeeks m

LEFT JOIN [TimeTracking] p ON 
(m.[WeeksSundayToSaturday] BETWEEN p.Date AND p.Date + 7)

RIGHT JOIN [DepartmentMembers] d ON
d.Resource = p.CreatedBy 


GROUP BY
d.Resource
,m.WeeksSundayToSaturday

Data Tables

Department Members      
Name    Department  
Bob     Engineer    
Sue     HR  
John    Operations  

Time Tracking       
Resource    Hours   Date
Bob         13      2/9/2014
Sue         12      2/10/2014
John        2       2/11/2014
Bob         6       2/12/2014
Bob         8       2/13/2014
John        8       2/14/2014
John        8       2/15/2014
Bob         8       2/16/2014
Bob         1       2/17/2014
Bob         2       2/18/2014
Bob         1       2/19/2014
Bob         8       2/20/2014
Bob         9       2/21/2014
Bob         6       2/22/2014
Sue         8       2/23/2014
John        2       2/24/2014

Calendar        
WeeksSundayToSaturday       
1/5/2014        
1/12/2014       
1/19/2014       
1/26/2014       
2/2/2014        
2/9/2014        
2/16/2014       
2/23/2014       
3/2/2014        
3/9/2014        
3/16/2014       
3/23/2014       
3/30/2014       

Desired Result

 Bob
  Week 7 = 27
  Week 8 = 35
  Week 9 = NULL

No correct solution

OTHER TIPS

Your above query is giving compilation error, please try below query i think it will help you

SELECT d.Resource ,SUM(p.Hours) AS Hours ,m.[WeeksSundayToSaturday] ,DatePart(wk, m.[WeeksSundayToSaturday]) AS WeekNumber

FROM CalendarWeeks m

LEFT JOIN [TimeTracking] p ON (p.Date BETWEEN m.[WeeksSundayToSaturday] AND Dateadd(d,6, m.[WeeksSundayToSaturday])

RIGHT JOIN [DepartmentMembers] d ON d.Resource = p.CreatedBy

GROUP BY d.Resource ,m.WeeksSundayToSaturday

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top