我在让 SQL 语句按我需要的方式工作时遇到问题。老实说,当谈到 SQL 时,我是非常绿色的,所以我尝试的尝试来自复制/粘贴代码,我试图编辑它以使其工作并且它没有运行。所以我需要的是一个用于 ACCESS 报告的查询。
以下是数据的样子:
ID TechID OccurrenceDate OccurrenceName OccurrenceAmt
247 9991 Friday, February 15, 2013 Coaching 4.50
242 9991 Friday, February 08, 2013 Con't Occurrence 0.00
241 9991 Thursday, February 07, 2013 Unscheduled Absense 1.00
240 9991 Wednesday, February 06, 2013 Shift Int less 2 hrs 0.50
243 9991 Monday, February 04, 2013 Unscheduled Absense 1.00
246 9991 Monday, January 21, 2013 Unscheduled Absense 1.00
245 9991 Wednesday, January 16, 2013 Con't Occurrence 0.00
244 9991 Tuesday, January 15, 2013 Unscheduled Absense 1.00
239 9999 Friday, February 08, 2013 Unscheduled Absense 1.00
237 9999 Wednesday, February 06, 2013 Unscheduled Absense 1.00
238 9999 Saturday, February 02, 2013 Coaching 7.00
236 9999 Tuesday, September 11, 2012 Other 6.00
235 9999 Tuesday, September 11, 2012 Other 0.00
228 9999 Thursday, August 23, 2012 Unscheduled Absense 1.00
227 9999 Friday, August 10, 2012 Unscheduled Absense 1.00
226 9999 Wednesday, August 08, 2012 Con't Occurrence 0.00
223 9999 Wednesday, February 29, 2012 Unscheduled Absense 1.00
249 9998 Saturday, February 02, 2013 Unscheduled Absense 1.00
251 9998 Monday, January 21, 2013 Unscheduled Absense 1.00
因此,基本上,如果在过去 6 个月内出现“Coaching”或“Other”的“OccurrenceName”,则该金额加上过去 6 个月内的任何其他事件应该是他们的技术总数。如果在过去 6 个月内没有发生“指导”或“其他”事件,那么我需要将前 6 个月滚动的 OccurrenceAmount 相加。
希望我很好解释的场景是有意义的。
编辑#1: 好的,我对此数据的预期输出应该是:
TechID Total
9991 4.5
9999 9.0
9998 2.0
如您所见,TechID 9991 的计算结果为 4.5,因为在过去 6 个月内发生了“教练”事件,但没有任何内容。9999 将有 9 次,因为在过去 6 个月内有 7 次辅导,此后又有两次,使总数达到 9。9998 有 2 次,因为该技术在过去 6 个月内没有任何辅导或任何东西,所以总数为 2。
编辑#2: 所以唯一应该计算的行是缩进的行。对于 9999,有 7 次和 2 次以上的常规训练,使他的总数达到 9。这更清楚吗?
编辑#3:
好的,走得更远一点。
@lance - 通过反复试验,我越来越接近......现在有这个,但无法让它工作:
SELECT tblEmployeeData.TechID, tblEmployeeData.LName, tblEmployeeData.FName, Sum(tblOccurrence.OccurrenceAmt), Last(tblOccurrence.CoachingDate) AS LastOfCoachingDate, tblEmployeeData.SupLName
FROM tblEmployeeData RIGHT JOIN tblOccurrence ON tblEmployeeData.TechID = tblOccurrence.TechID
GROUP BY tblEmployeeData.TechID, tblEmployeeData.LName, tblEmployeeData.FName, tblEmployeeData.SupLName
HAVING (((tblOccurrence.OccurrenceAmt))=IIf([tblOccurrence].[CoachingDate]="",[tblOccurrence].[OccurrenceDate] Between Date() And DateAdd('m',-6,Date()),IIf([tblOccurrence].[CoachingDate]<=DateAdd('m',-6,Date()),[tblOccurrence].[OccurrenceDate] Between Date() And DateAdd('d',[tblOccurrence].[CoachingDate],Date()))));
编辑#4: 这个查询是我开始工作的“最好的”开始查询。它提取所有员工数据,然后填充 MaxCoaching 和 MaxDate。因此,我尝试将此查询连接到您的第二个查询以获取查询的总数,但无法使其正常工作。
询问:
SELECT tblEmployeeData.TechID, tblEmployeeData.LName, tblEmployeeData.FName, Max(tblOccurrence.CoachingDate) AS LastCoachingDate, Max([OccurrenceDate]) AS MaxDate, tblEmployeeData.SupLName
FROM tblEmployeeData LEFT JOIN tblOccurrence ON tblEmployeeData.TechID = tblOccurrence.TechID
GROUP BY tblEmployeeData.SupLName, tblEmployeeData.TechID, tblEmployeeData.LName, tblEmployeeData.FName
所以这些结果得到最近的教练日期(如果有的话)和最近的事件日期,所以我需要根据 2 个条件对发生次数求和:
- 如果在过去 6 个月内有辅导/其他日期,则需要该行的发生总数加上辅导/其他日期之后发生的任何其他日期
- 如果过去 6 个月内没有发生任何辅导/其他日期,那么我需要过去 6 个月内发生的总次数。
更接近于获得有效的查询!谢谢你的帮助