0

我在让 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 个条件对发生次数求和:

  1. 如果在过去 6 个月内有辅导/其他日期,则需要该行的发生总数加上辅导/其他日期之后发生的任何其他日期
  2. 如果过去 6 个月内没有发生任何辅导/其他日期,那么我需要过去 6 个月内发生的总次数。

更接近于获得有效的查询!谢谢你的帮助

4

1 回答 1

0

@Zamael 如果我理解您的问题和说明,那么 9998 的总和为 9 的原因是 9 月的日期是 6 个月前,并且还有另一个 OccurenceAmt 为 2 的行项目,我们在桌子。

假设这是正确的,我建议您使用以下 SQL 创建一个查询:

Select TechID, 
       SUM(IIF([OccurenceName] in ('Coaching','Other'),1,0)) as CoachingOtherCount,     
       SUM(IIF([OccurenceName] in ('Coaching','Other'),OccurenceAmt,0)) as CoachingAmt, 
       SUM(IIF(IIF([OccurenceName] in ('Coaching','Other'),0,OccurenceAmt))) as MiscAmt
From TableName
Where OccurenceDate > = DateAdd("m",-6,Date())
Group By TechID;

然后,使用以下 SQL 创建第二个查询:

select TechID,
       IIF(Nz(CoachingOtherCount,0) > 0, CoachingAmt, MiscAmt) as SumOccurenceAmt
from Query1;

编辑 1 - 澄清后 我现在理解的是: 第 1 步:在过去 6 个月内找到“教练”或“其他”。如果存在,则转到 2,否则转到 3。 第 2 步:求和第 1 步中最早出现的所有行。 第 3 步:对过去 6 个月的所有行求和。

如果这是您要查找的内容,则以下代码将起作用。

SELECT TechID, 
       MIN(IIF([OccurrenceName] in ('Coaching','Other'),OccurrenceDate,NULL)) AS MinCoachingOtherDate, 
       MIN([OccurrenceDate]) AS MinDate
FROM tblOccurrence
WHERE OccurrenceDate >= DateAdd("m",-6,Date())
GROUP BY TechID;

然后,在第二个查询中

Select tbo.TechID,
       SUM([OccurrenceAmt]) as Amount

From tblOccurrence tbo
     LEFT JOIN QUERY1 q on q.techid = tbo.techid

WHERE tbo.OccurrenceDate >= IIF(q.MinCoachingOtherDate IS NULL, q.MinDate, q.MinCoachingOtherDate)

GROUP BY tbo.TechID;

第一个查询在第一列中查找 Coaching/Other 的最早日期(如果存在),并在第二列中查找最早的日期。然后,在第二个查询中,我们假设 Coaching/Other 日期,除非它为空,然后我们默认为第二个日期。因为 where 动态约束每个 TechID 的日期,所以您可以将所有 OccurenceAmt 相加。

于 2013-04-22T16:34:27.137 回答