我有一个表 tempslotsassignedfordept ,如下所示
DEPT ScheduledStartDate ScheduledEndDate TotalSlots
DevEng 2012-07-17 00:00:00.000 2012-08-28 00:00:00.000 39
DevEng 2012-07-17 00:00:00.000 2012-08-31 00:00:00.000 18
DevEng 2012-07-18 00:00:00.000 2012-08-29 00:00:00.000 9
DevEng 2012-07-19 00:00:00.000 2012-08-30 00:00:00.000 40
Prod 2012-07-19 00:00:00.000 2012-08-30 00:00:00.000 8
Rel 2012-07-19 00:00:00.000 2012-08-30 00:00:00.000 19
Rel 2012-07-19 00:00:00.000 2013-08-15 00:00:00.000 1
我必须捕获每个部门总数的上述总和。如果某个部门存在连续的3天 如果您看到上表,有部门deveng,则序列三预定开始日期为2012-07-17,2012-07-18,2012-07-19。
我试图通过使用下面的查询来查看结果,请帮助您查看确切结果的更多实现是什么?
SELECT Tmain.DepartmentCode,
CONVERT(VARCHAR(20), Tmain.ScheduledStartDate, 101) 'ScheduledStartDate',
SUM(Tmain.TotalSlots) 'Tmain.TotalSlots',T1Sub.TotalSlots 'T1SubTotalSlots', T2Sub.TotalSlots 'T2SubTotalSlots'
FROM tempSlotsAssignedForDept Tmain
INNER JOIN (Select DATEADD(DAY,1,T1.ScheduledStartDate) 'ScheduledStartDate',SUM(T1.TotalSlots) 'TotalSlots'
FROM tempSlotsAssignedForDept T1
INNER JOIN tempSlotsAssignedForDept Tmain
ON (T1.ScheduledStartDate = dateadd(day, -1, tmain.ScheduledStartDate ) and
T1.DepartmentCode = tmain.DepartmentCode)
GROUP BY T1.ScheduledStartDate ) T1Sub
ON Tmain.ScheduledStartDate = T1Sub.ScheduledStartDate
INNER JOIN (Select DATEADD(DAY,2,T2.ScheduledStartDate) 'ScheduledStartDate',SUM(T2.TotalSlots) 'TotalSlots'
FROM tempSlotsAssignedForDept T2
INNER JOIN tempSlotsAssignedForDept Tmain
ON (T2.ScheduledStartDate = dateadd(day, -2, tmain.ScheduledStartDate ) and
T2.DepartmentCode = tmain.DepartmentCode)
GROUP BY T2.ScheduledStartDate ) T2Sub
ON Tmain.ScheduledStartDate = T2Sub.ScheduledStartDate
GROUP BY Tmain.DepartmentCode,Tmain.ScheduledStartDate,T1Sub.TotalSlots , T2Sub.TotalSlots
ORDER BY Tmain.ScheduledStartDate
Outof the Above查询
DepartmentCode ScheduledStartDate Tmain.TotalSlots T1SubTotalSlots T2SubTotalSlots
DevEng 07/19/2012 40 9 57
Prod 07/19/2012 8 9 57
Rel 07/19/2012 20 9 57
最终结果如下:
因为部门刺激和相关人员没有为3天安排的系列计划
DepartmentCode ScheduledStartDate Tmain.TotalSlots T1SubTotalSlots T2SubTotalSlots
DevEng 07/19/2012 40 9 57
请帮忙。
答案 0 :(得分:1)
以下是您正在寻找的答案:
Select * from
(
Select DepartmentCode,ScheduledStartDate,[Tmain.TotalSlots]
,(Select SUM(T1.TotalSlots) from tempSlotsAssignedForDept T1 where t1.departmentCode=a.DepartmentCode
and T1.ScheduledStartDate = dateadd(day, -1, a.ScheduledStartDate )) T1SubTotalSlots
,(Select SUM(T1.TotalSlots) from tempSlotsAssignedForDept T1 where t1.departmentCode=a.DepartmentCode
and T1.ScheduledStartDate = dateadd(day, -2, a.ScheduledStartDate )) T2SubTotalSlots
from
(
SELECT Tmain.DepartmentCode,
CONVERT(VARCHAR(20), Tmain.ScheduledStartDate, 101) ScheduledStartDate,
SUM(Tmain.TotalSlots) 'Tmain.TotalSlots'
--,(Select SUM(T1.TotalSlots) from tempSlotsAssignedForDept t1 where t1.DepartmentCode=tmain.departmentcode
--and T1.ScheduledStartDate = dateadd(day, -1, tmain.ScheduledStartDate ))
FROM tempSlotsAssignedForDept Tmain
GROUP BY Tmain.DepartmentCode,Tmain.ScheduledStartDate
) a) b where [Tmain.TotalSlots] is not null and T1SubTotalSlots is not null and T2SubTotalSlots is not null