我正在努力展示前一周没有进入任何时间的员工。我目前正在使用三张桌子。一个表是具有每周第一个日期的日历。周格式为周日至周六。第二个表是输入的小时列表。时间表包含输入时间的日期和员工姓名。第三个表是所有员工的列表。我似乎无法让联接工作,我希望他们如何。最终结果我想看到Bob在第7周和第8周输入时间,但第9周为空。谢谢您的帮助。非常感谢。
当前代码
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
数据表
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
期望的结果
Bob
Week 7 = 27
Week 8 = 35
Week 9 = NULL
答案 0 :(得分:0)
您的上述查询给出了编译错误,请尝试以下查询我认为它会对您有所帮助
选择 d.Resource ,SUM(p.Hours)AS Hours ,米。[WeeksSundayToSaturday] ,DatePart(周,周[周刊周刊])AS周数
FROM CalendarWeeks m
LEFT JOIN [TimeTracking] p ON(p.Date BETWEEN m。[WeeksSundayToSaturday]和Dateadd(d,6,m。[WeeksSundayToSaturday])
RIGHT JOIN [DepartmentMembers] d ON d.Resource = p.CreatedBy
GROUP BY d.Resource,m.WeeksSundayToSaturday