我有下表(但有更多数据):
Workcenter ID | Status | Start Date/Time | End Date/Time SLX83D Breakdown 11/27/2016 16:00:00 11/29/2016 12:30:00 SLX83C No Operator 11/27/2016 6:40:00 11/27/2016 13:00:00 SLX83A QA Inspection 11/28/2016 13:00:00 11/28/2016 14:00:00 SLX83A QA Inspection 11/28/2016 15:00:00 11/28/2016 16:00:00
我想按工作日(即12/1 6am到12/2 6am)和总和分开状态时间间隔。
我的想法是按照以下格式创建交叉表查询:
Work Day | Workcenter ID | Breakdown | No Operator | QA Inspection 11/27/2016 SLX83D 14:00:00 11/28/2016 SLX83D 24:00:00 11/29/2016 SLX83D 6:30:00 11/27/2016 SLX83C 6:20:00 11/28/2016 SLX83A 2:00:00
无法弄清楚如何使其发挥作用,我对其他想法持开放态度。
PS我的表中还包含以下列:开始日期,开始时间,结束日期和结束时间。(这些用于计算“开始日期/时间”和“结束日期/时间”
我对Access相对较新,对SQL几乎一无所知,谢谢。
编辑:通过在表中创建以下计算列,我能够找到给定日期的状态:
在日期之间: IIf([Start Date/Time]>= "11/28/2016 06:00:00" And [End Date/Time]<= "11/29/2016 06:00:00" ,[End Date/Time]-[Start Date/Time],0)*24*60
开始日期: IIf([Start Date/Time]< "11/28/2016 06:00:00" And [End Date/Time]>= "11/28/2016 06:00:00" And [End Date/Time]<= "11/29/2016 06:00:00" ,[End Date/Time]- "11/28/2016 06:00:00" ,0)*24*60
在日期之后结束: IIf([End Date/Time]> "11/28/2016 06:00:00" And [Start Date/Time]>= "11/28/2016 06:00:00" And [Start Date/Time]<= "11/29/2016 06:00:00" , "11/29/2016 06:00:00" -[Start Date/Time],0)*24*60
跨度整个日期: IIf([Start Date/Time]< "11/28/2016 06:00:00" And [End Date/Time]> "11/29/2016 06:00:00" ,1,0)*24*60
汇总这些列可得出给定工作中心在给定日期具有给定状态的总分钟数。
但是,这一次仅适用于一个工作日。我宁愿弄清楚如何使用SQL来获得如上表所示的工作日历史记录的结果。
Workcenter ID | Status | Start Date/Time | End Date/Time SLX83D Breakdown 11/27/2016 16:00:00 11/29/2016 12:30:00 SLX83A QA Inspection 11/28/2016 13:00:00 11/28/2016 14:00:00 SLX83A QA Inspection 11/28/2016 15:00:00 11/28/2016 16:00:00
Workcenter ID | IBD | SBD | EAD | SWD | SUM SLX83D 1440 1440 (24 hrs) SLX83A 60 60 (1 hr) SLX83A 60 60 (These 2 hrs would have to be summed later somehow, this isn't a great solution)
编辑2:我尝试了以下操作,但无法正常工作。(信用转到:https : //social.msdn.microsoft.com/Forums/sqlserver/en-US/cca28ddd-2041-4504-b602-3bedd9ca704e/counts-by-time-interval?forum=transactsql#208e97ad-11a7 -4d96-9436-c82612755d46)
SELECT DatePart(Day,[Start Date/Time]) AS [Workday Date], Count([Status Table].[Workcenter ID]) AS [Workday Interval]
FROM [Status Table]
WHERE ((([Status Table].[Start Date/Time]) Between [Start Date/Time] And [End Date/Time]))
GROUP BY DatePart(Day,interval);
考虑分多个阶段处理您的需求:
建立一个时间表表,其中包含所有可能的工作日开始时间(第一天6:00 AM)和结束点(第二天5:59 AM)。如果天数达到数百或数千,则可能需要使用VBA循环从范围的最早到最新日期动态创建一个新表(TableDef)。
联合查询结合了所有三个可能的持续时间:a)一天的部分开始;b)整整一天;c)部分一天结束。每个都使用嵌套IIF()
逻辑来计算小时数。每个SELECT
查询将使用“实际小时数”表和“时间表”表运行交叉连接和间隔过滤器。
横标签查询以在变换后的输出由分组集合WorkDates和ID,通过枢转状态列。
联合查询 (请确保删除注释,这些注释不符合Access SQL查询语句)。
-- ACTUAL START PARTIAL
SELECT w.[Workcenter ID], w.Status, w.[StartDate/Time], w.[End Date/Time],
s.WorkStartDates, s.WorkEndDates,
IIF(w.[StartDate/Time] >= s.WorkStartDates AND w.[End Date/Time] <= s.WorkEndDates,
DateDiff('n', w.[StartDate/Time], w.[End Date/Time]),
IIF(w.[StartDate/Time] >= s.WorkStartDates AND w.[End Date/Time] >= s.WorkEndDates,
DateDiff('n', w.[StartDate/Time], s.WorkEndDates), NULL)) / 60 AS WorkHours
FROM WorkCenterHours w, Schedule s
WHERE w.[StartDate/Time] BETWEEN s.WorkStartDates AND s.WorkEndDates
UNION
-- ACTUAL START AND END ACROSS WORKDAYS
SELECT w.[Workcenter ID], w.Status, w.[StartDate/Time], w.[End Date/Time],
s.WorkStartDates, s.WorkEndDates,
DateDiff('n', s.WorkStartDates, s.WorkEndDates) / 60 As WorkHours
FROM WorkCenterHours w, Schedule s
WHERE s.WorkStartDates BETWEEN w.[StartDate/Time] AND w.[End Date/Time]
AND s.WorkEndDates BETWEEN w.[StartDate/Time] AND w.[End Date/Time]
UNION
-- ACTUAL END LEFTOVER
SELECT w.[Workcenter ID], w.Status, w.[StartDate/Time], w.[End Date/Time],
s.WorkStartDates, s.WorkEndDates,
IIF(w.[StartDate/Time] <= s.WorkStartDates AND w.[End Date/Time] <= s.WorkEndDates,
DateDiff('n', w.[End Date/Time], s.WorkEndDates),
IIF(w.[StartDate/Time] >= s.WorkStartDates AND w.[End Date/Time] <= s.WorkEndDates,
DateDiff('n', w.[StartDate/Time], w.[End Date/Time]), NULL)) / 60 AS WorkHours
FROM WorkCenterHours w, Schedule s
WHERE w.[End Date/Time] BETWEEN s.WorkStartDates AND s.WorkEndDates;
交叉表查询 (用作数据源)
TRANSFORM Sum(q.WorkHours) AS SumOfWorkHours
SELECT DateValue(q.WorkStartDates) AS WorkDate, q.[Workcenter ID]
FROM UnionSourceQuery q
GROUP BY DateValue(q.WorkStartDates), q.[Workcenter ID]
PIVOT q.Status;
注意:由于使用嵌套逻辑和各种日期函数调用的并集查询中的交叉联接筛选器,因此不能保证性能,并且此解决方案的性能可能随数据大小而变化,然后最终受到影响。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句