将时间加和分组成几个小时,使60分钟

时间:2018-08-09 07:01:54

标签: sql sql-server sql-server-2008 tsql

我在矿山工作,我的任务是要说明挖掘机每小时在特定状态下花费的时间和原因。我们每天运行3个班次(6-14; 14-22; 22-6)。每个挖掘机的时间总计应每小时达到60分钟(我的分钟专栏)。目前,我只是想让一个挖掘机在一个班次(班次1,“日班”)中加起来,然后将其扩展为包括所有挖掘机。我设法获得了第一个小时的工作时间(6-7)。问题是有些事件会持续多个小时。因此,从本质上讲,我正在尝试为跨越多个小时的事件复制数据。因此,例如,如果一个事件跨越2个小时(6-7和7到8),则事件的总时间必须分为2行,每行都占我在该小时中花费的时间的一部分。

注意:结束时间和开始时间以距离班次开始的秒数为单位,持续时间也以秒为单位。

我当前的查询和输出如下。如您所见,我在第2点(7-8)陷入困境:

declare @date as datetime
set @date = (select max(shiftdate) -1 from hist_exproot) 

select shiftdate
    ,shift
    ,rtrim(eqmt) as eqmt
    ,dateadd(second,start+starttime,shiftdate) as event_start
    ,starttime
    ,dateadd(second,start+endtime,shiftdate) as event_end
    ,endtime

    ,case when endtime >= 3600 and starttime < 3600 then (3600 - starttime)/60
        when endtime >= 3600 and starttime < 3600 and endtime < 7200 then (endtime - 3600)/60
        when endtime >= 7200 and starttime between 3600 and 7200 then (7200 - starttime)/60
     else (endtime - starttime)/60 end as minutes

    ,duration/60 as duration

    ,case when hs.category = 1 then 'Ready'
        when hs.category = 2 then 'Utility'
        when hs.category = 3 then 'Delay'
        when hs.category = 4 then 'Standby'
        when hs.category = 5 then 'Scheduled Down'
        when hs.category = 6 then 'Unscheduled Down'
    else 'ukn' end as category

,case when starttime < 3600 and shift# = 1 then '07'
when starttime < 3600 and endtime > 3600 and endtime < 7200 and shift# = 1 then '08'
when starttime >= 3600 and endtime < 7200 and shift# = 1 then '08'
when starttime between 3600 and 7200 and endtime > 7200 and shift# = 1 then '08'
else 'ukn' end as hour

,rtrim(hr.name) as reason 
from hist_statusevents as hs 
inner join hist_reasontable as hr on 
hr.shiftindex = hs.shiftindex and 
hr.reason = hs.reason
inner join hist_exproot as he on 
he.shiftindex = hs.shiftindex 

where shiftdate = @date
and eqmt = 'EX04'
and hr.name not like 'mmsunk'
and shift# = 1
order by  asc

结果:

+---------------------+-----------+------+---------------------+-----------+---------------------+---------+------------------+------------------+----------+------+-------------------------+
|      shiftdate      |   shift   | eqmt |     event_start     | starttime |      event_end      | endtime |     minutes      |     duration     | category | hour |         reason          |
+---------------------+-----------+------+---------------------+-----------+---------------------+---------+------------------+------------------+----------+------+-------------------------+
| 2018-08-08 00:00:00 | Day Shift | EX04 | 2018-08-08 06:00:00 |         0 | 2018-08-08 06:20:00 |    1215 |            20.25 |            20.25 | Ready    | 07   | Production              |
| 2018-08-08 00:00:00 | Day Shift | EX04 | 2018-08-08 06:20:00 |      1215 | 2018-08-08 06:27:00 |    1607 | 6.53333333333333 | 6.53333333333333 | Standby  | 07   | SHIFT CHANGE            |
| 2018-08-08 00:00:00 | Day Shift | EX04 | 2018-08-08 06:27:00 |      1607 | 2018-08-08 06:29:00 |    1710 | 1.71666666666667 | 1.71666666666667 | Standby  | 07   | SHIFT CHANGE            |
| 2018-08-08 00:00:00 | Day Shift | EX04 | 2018-08-08 06:29:00 |      1710 | 2018-08-08 07:12:00 |    4298 |             31.5 | 43.1333333333333 | Ready    | 07   | Production              |
| 2018-08-08 00:00:00 | Day Shift | EX04 | 2018-08-08 07:12:00 |      4298 | 2018-08-08 07:15:00 |    4493 |             3.25 |             3.25 | Utility  | 08   | Clean Dig/Work Area     |
| 2018-08-08 00:00:00 | Day Shift | EX04 | 2018-08-08 07:15:00 |      4493 | 2018-08-08 08:23:00 |    8598 | 45.1166666666667 | 68.4166666666667 | Ready    | 08   | Production              |
| 2018-08-08 00:00:00 | Day Shift | EX04 | 2018-08-08 08:23:00 |      8598 | 2018-08-08 08:31:00 |    9036 |              7.3 |              7.3 | Delay    | ukn  | Check Maps/Instructions |
| 2018-08-08 00:00:00 | Day Shift | EX04 | 2018-08-08 08:31:00 |      9036 | 2018-08-08 09:54:00 |   14029 | 83.2166666666667 | 83.2166666666667 | Ready    | ukn  | Production              |
| 2018-08-08 00:00:00 | Day Shift | EX04 | 2018-08-08 09:54:00 |     14029 | 2018-08-08 11:51:00 |   21034 |           116.75 |           116.75 | Utility  | ukn  | Move for Blast          |
| 2018-08-08 00:00:00 | Day Shift | EX04 | 2018-08-08 11:51:00 |     21034 | 2018-08-08 14:00:00 |   28800 | 129.433333333333 | 129.433333333333 | Delay    | ukn  | Blasting                |
+---------------------+-----------+------+---------------------+-----------+---------------------+---------+------------------+------------------+----------+------+-------------------------+

样本数据:

历史理由:

shiftindex  cliID   ddbkey  name    reason  status  delaytime   category    mainttime   altname center  isfromshi#  isfromshi
53258   31172   472666  STRUCTURE-OTHER 420 1   1200    6   NO  1   1   NULL    01. Availability
53258   31172   472886  SUSPENSION  450 1   1200    6   NO  0   0   NULL    01. Availability
53258   31172   475856  SWING SYSTEM    1900    1   1200    6   NO  1   1   NULL    01. Availability
53258   31172   477616  Scheduled Meeting   5410    3   1200    4   NO  1   1   NULL    02. Utilisation
53258   31172   484656  Service Equip   6410    4   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   484436  Shift Break 6205    4   1200    3   NO  1   1   NULL    02. Utilisation
53258   31172   482786  Short Move 10 Mins  5505    4   600 2   NO  1   1   NULL    03. Effectiveness
53258   31172   478166  Strike  5625    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   474866  TRAMMING SYSTEM 1500    1   1200    6   NO  1   1   NULL    01. Availability
53258   31172   473216  TRANSMISSION    645 1   1200    6   NO  0   0   NULL    01. Availability
53258   31172   475966  TYRES   2000    1   1200    6   NO  1   1   NULL    01. Availability
53258   31172   476076  TYRES-ROCK EJECTOR  2010    1   1200    6   NO  0   0   NULL    01. Availability
53258   31172   482676  Talk to Supervisor  5415    4   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   485866  Toilet Stop 6820    4   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   485096  Traffic Delay   6710    4   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   485206  Travel to Fuel Bay  6715    4   1200    2   NO  1   1   NULL    03. Effectiveness
53258   31172   485536  Travel to Wash Bay  6730    4   1200    2   NO  0   0   NULL    03. Effectiveness
53258   31172   485426  Travel to Water Stand   6725    4   1200    2   NO  0   0   NULL    03. Effectiveness
53258   31172   485646  Travel to Work Area 6735    4   1200    2   NO  1   1   NULL    03. Effectiveness
53258   31172   485316  Travel to maintenance   6720    4   1200    2   NO  0   0   NULL    03. Effectiveness
53258   31172   1893682 UNDEF   6011    4   1200    3   NO  0   0   NULL    04. Ready
53258   31172   476186  UNDERCARRIAGE   2100    1   1200    6   NO  0   0   NULL    01. Availability
53258   31172   479926  WAITING ON SHOVEL   6935    3   1200    4   NO  1   1   NULL    02. Utilisation
53258   31172   474646  WATER SPRAY SYSTEM  1390    1   1200    6   NO  0   0   NULL    01. Availability
53258   31172   485976  Waiting for Fuel Bay    6905    4   1200    3   NO  1   1   NULL    02. Utilisation
53258   31172   477836  Waiting for Operator    5610    3   1200    4   NO  1   1   NULL    02. Utilisation
53258   31172   486086  Waiting for Pad 6910    4   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   486196  Waiting for Water   6915    4   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   479376  Waiting on Clean Up 6615    3   1200    4   NO  1   1   NULL    02. Utilisation
53258   31172   479596  Waiting on Equipment    6625    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   479706  Waiting on Supplies 6630    3   1200    4   NO  1   1   NULL    02. Utilisation
53258   31172   479816  Waiting on Tech Support 6635    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   484766  Watering Equip  6415    4   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   483556  Weight Scale    5920    4   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   482566  Work Area Inspection    5320    4   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   NULL    mmsunk  5127    3   0   4   no  NULL    NULL    NULL    NULL
53258   31172   474976  ACCIDENT DAMAGE 1605    1   1200    6   NO  0   0   NULL    01. Availability
53258   31172   472776  AIR CONDITIONING    435 1   1200    6   NO  1   1   NULL    01. Availability
53258   31172   472226  AIR SYSTEM  200 1   1200    6   NO  0   0   NULL    01. Availability
53258   31172   476956  Accident    5205    3   1200    4   NO  1   1   NULL    02. Utilisation
53258   31172   484326  Add/Change/reroute Power Cabl   6125    4   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   478386  Available but not Required  5705    3   1200    4   YES 1   1   NULL    02. Utilisation
53258   31172   472336  BRAKING 300 1   1200    6   NO  1   1   NULL    01. Availability
53258   31172   482126  Blasting    5105    4   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   472556  CABIN   410 1   1200    6   NO  1   1   NULL    01. Availability
53258   31172   483996  Change Bit  6105    4   1200    2   NO  0   0   NULL    03. Effectiveness
53258   31172   484106  Change Hammer   6110    4   1200    2   NO  0   0   NULL    03. Effectiveness
53258   31172   484216  Change Rod  6115    4   1200    2   NO  0   0   NULL    03. Effectiveness
53258   31172   477726  Changing Operators  5605    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   483666  Check Maps/Instructions 6005    4   1200    3   NO  1   1   NULL    02. Utilisation
53258   31172   481686  Clean Bed/Bucket    5005    4   600 2   NO  0   0   NULL    03. Effectiveness
53258   31172   481796  Clean Dig/Work Area 5010    4   1200    2   NO  1   1   NULL    03. Effectiveness
53258   31172   481906  Cleaning Cab    5015    4   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   482016  Cleaning Equip  5020    4   1200    3   NO  1   1   NULL    02. Utilisation
53258   31172   483336  Cooling Equipment/Tires/Opera   5910    4   1200    2   NO  0   0   NULL    03. Effectiveness
53258   31172   480036  Crusher waiting for Trucks  6950    3   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   476626  De_rated Production 110 2   1200    1   NO  0   0   NULL    04. Ready
53258   31172   481136  Dump Not Avail  7135    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   480476  Dust    7105    3   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   473326  ELECTRICAL  700 1   1200    6   NO  1   1   NULL    01. Availability
53258   31172   473436  ELECTRICAL-LIGHTING 820 1   1200    6   NO  1   1   NULL    01. Availability
53258   31172   473546  ELECTRICAL-POWER FAILURE    900 1   1200    6   NO  1   1   NULL    01. Availability
53258   31172   473106  ELECTRICAL-PROPULSION   640 1   1200    6   NO  0   0   NULL    01. Availability
53258   31172   473656  ELECTRICAL-ZESCO EXTERNAL   950 1   1200    6   NO  0   0   NULL    01. Availability
53258   31172   473766  ENGINE  1000    1   1200    6   NO  1   1   NULL    01. Availability
53258   31172   473876  ENGINE-COOLING SYSTEM   1010    1   1200    6   NO  1   1   NULL    01. Availability
53258   31172   479266  Emergency Drill 6610    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   477506  Employee Eval   5405    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   482236  Equipment Inspection    5305    4   1200    3   NO  1   1   NULL    02. Utilisation
53258   31172   486306  Equipment Stuck 6920    4   1200    3   NO  1   1   NULL    02. Utilisation
53258   31172   484876  Equipment Training  6605    4   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   477066  Evacuation  5210    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   482346  FLRA(Risk Assessment)   5310    4   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   485756  Fatigue 6805    4   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   483446  Filling Water Tank  5915    4   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   480256  Flooding/Rain   7010    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   480366  Fog 7020    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   484546  Fuel Equip  6405    4   1200    3   NO  1   1   NULL    02. Utilisation
53258   31172   486416  GPS Coverage    6925    4   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   474756  GREASE SYSTEM   1405    1   1200    6   NO  1   1   NULL    01. Availability
53258   31172   477176  Ground Conditions   5215    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   474096  HYDRAULIC   1200    1   1200    6   NO  1   1   NULL    01. Availability
53258   31172   474206  IMPLEMENTS-BLADE    1310    1   1200    6   NO  1   1   NULL    01. Availability
53258   31172   474316  IMPLEMENTS-BUCKET   1325    1   1200    6   NO  1   1   NULL    01. Availability
53258   31172   476296  IMPLEMENTS-GET  2230    1   1200    6   NO  0   0   NULL    01. Availability
53258   31172   481246  Illegal Miners  7140    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   480146  Lightning   7005    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   483006  Long Move 60 Mins   5515    4   3600    2   NO  1   1   NULL    03. Effectiveness
53258   31172   475416  MAJOR-SHUTDOWN  1740    1   1200    5   NO  1   1   NULL    01. Availability
53258   31172   474426  MAST    1365    1   1200    6   NO  1   1   NULL    01. Availability
53258   31172   472996  MECHANICAL DRIVE    600 1   1200    6   NO  1   1   NULL    01. Availability
53258   31172   483776  Marking  Holes  6010    4   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   483886  Measure Holes   6015    4   1200    3   NO  0   0   NULL    02. Utilisation
53258   31172   476736  Mechanically De_Rated   115 2   1200    1   NO  0   0   NULL    04. Ready
53258   31172   482896  Medium Move 30 Mins 5510    4   1800    2   NO  1   1   NULL    03. Effectiveness
53258   31172   477286  Mine Shut Down  5220    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   483116  Move for Blast  5520    4   1200    2   NO  0   0   NULL    03. Effectiveness
53258   31172   483226  Move to Pattern 5525    4   1200    2   NO  0   0   NULL    03. Effectiveness
53258   31172   479486  No Access to Crusher    6620    3   1200    4   NO  1   1   NULL    02. Utilisation
53258   31172   478496  No Ore Control  5805    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   480586  No Pattern/Round    7110    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   480696  No Support Avail    7115    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   478826  No Support Equipment    5825    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   477946  Not Enough Operators    5615    3   1200    4   NO  1   1   NULL    02. Utilisation
53258   31172   481466  Not Enough Shovels - Breakdow   7150    3   1200    4   NO  1   1   NULL    02. Utilisation
53258   31172   481026  Not Enough Shovels - Operator   7130    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   481576  Not Enough Shovels - Relocati   7155    3   1200    4   NO  1   1   NULL    02. Utilisation
53258   31172   481356  Not Enough Trucks - Breakdown   7145    3   1200    4   NO  1   1   NULL    02. Utilisation
53258   31172   480806  Not Enough Trucks - Operators   7120    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   475086  OTHER-FIRE SUPRESSION   1620    1   1200    6   NO  0   0   NULL    01. Availability
53258   31172   473986  OTHER-FUEL TANK 1025    1   1200    6   NO  1   1   NULL    01. Availability
53258   31172   476846  Other Operation 5920    2   1200    2   NO  1   1   NULL    03. Effectiveness
53258   31172   478606  Other Operations    5810    3   1200    2   NO  1   1   NULL    03. Effectiveness
53258   31172   480916  Out of Material 7125    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   479046  Parked  6310    3   1200    4   NO  1   1   NULL    02. Utilisation
53258   31172   477396  Poor Ventilation    5225    3   1200    4   NO  0   0   NULL    02. Utilisation
53258   31172   482456  Prestart    5315    4   1200    3   NO  1   1   NULL    02. Utilisation
53258   31172   476406  Production  100 2   1200    1   NO  1   1   NULL    04. Ready
53258   31172   474536  ROTARY HEAD 1375    1   1200    6   NO  0   0   NULL    01. Availability
53258   31172   476516  Rehandle    5815    3   1200    2   NO  0   0   NULL    03. Effectiveness
53258   31172   478936  Restocking  5830    3   1200    2   NO  0   0   NULL    03. Effectiveness
53258   31172   484986  Restricted Access   6705    4   1200    3   NO  1   1   NULL    02. Utilisation
53258   31172   478056  Return From Unplanned Mainten   5620    3   1200    4   NO  1   1   NULL    02. Utilisation
53258   31172   478276  Returned from Planned Mainten   5630    3   1200    4   YES 1   1   NULL    02. Utilisation
53258   31172   478716  Rework  5820    3   1200    2   NO  0   0   NULL    03. Effectiveness
53258   31172   475306  SCH-BACKLOG 1735    1   1200    5   NO  1   1   NULL    01. Availability
53258   31172   475526  SCH-INSPECTION  1745    1   1200    5   NO  1   1   NULL    01. Availability
53258   31172   475196  SCH-PREVENTIVE MAINTENANCE  1705    1   1200    5   NO  1   1   NULL    01. Availability
53258   31172   475636  SCH-TYRE RETORQUE   1750    1   1200    5   NO  0   0   NULL    01. Availability
53258   31172   479156  SHIFT CHANGE    6315    3   1200    4   NO  1   1   NULL    02. Utilisation
53258   31172   475746  STEERING    1800    1   1200    6   NO  0   0   NULL    01. Availability
53258   31172   472446  STRUCTURE-CHASSIS   400 1   1200    6   NO  0   0   NULL    01. Availability

hist_statusevents:

shiftindex  cliID   ddbkey  eqmt    unit    operid  starttime   endtime duration    reason  status  category    comment vEvent  reasonlink  stdcomm
53258   31172   1940912 EX04    2   000053  3256    3498    242 100 2   1       0   476406  Other
53258   31172   1941292 EX04    2   000053  3715    8973    5258    100 2   1       1   476406  Other
53258   31172   1941672 EX04    2   000053  9154    9711    557 100 2   1       1   476406  Other
53258   31172   1942052 EX04    2   000053  10047   18001   7954    100 2   1       1   476406  Other
53258   31172   1942432 EX04    2   000053  19227   27286   8059    100 2   1       1   476406  Other
53258   31172   1942622 EX04    2   000053  27286   28800   1514    5410    3   4       1   477616  Other
53258   31172   1941482 EX04    2   000053  8973    9154    181 5010    4   2       1   481796  Other
53258   31172   1941862 EX04    2   000053  9711    10047   336 5505    4   2       1   482786  Other
53258   31172   1941102 EX04    2   000053  3498    3715    217 6005    4   3       1   483666  Other
53258   31172   1942242 EX04    2   000053  18001   19227   1226    6205    4   3       1   484436  Other
53258   31172   1940532 EX04    2   000562  1736    2841    1105    100 2   1       1   476406  Other
53258   31172   1940342 EX04    2   000562  1580    1736    156 6315    3   4       0   479156  Other
53258   31172   1939962 EX04    2   mmsunk  0   742 742 100 2   1       0   476406  Other
53258   31172   1940722 EX04    2   mmsunk  2841    3256    415 100 2   1       0   476406  Other
53258   31172   1940152 EX04    2   mmsunk  742 1580    838 6315    3   4       1   479156  Other

预期的结果,您会注意到一行重复,但是在“分钟”列中,它应该仅计算从前一小时结束算起的分钟。这是一个跨越两个小时的事件,在“小时”列中,现在是8,表示下一个小时:

shiftdate   shift   eqmt    event_start starttime   event_end   endtime minutes duration    category    hour    reason
8/9/2018 0:00   Day Shift   EX04    8/9/2018 6:00   0   8/9/2018 6:18   1102    18.36666667 18.36666667 Ready   7   Production
8/9/2018 0:00   Day Shift   EX04    8/9/2018 6:18   1102    8/9/2018 6:31   1876    12.9    12.9    Standby 7   SHIFT CHANGE
8/9/2018 0:00   Day Shift   EX04    8/9/2018 6:31   1876    8/9/2018 6:40   2381    8.416666667 8.416666667 Standby 7   SHIFT CHANGE
8/9/2018 0:00   Day Shift   EX04    8/9/2018 6:40   2381    8/9/2018 7:26   5185    20.31666667 46.73333333 Ready   7   Production
8/9/2018 0:00   Day Shift   EX04    8/9/2018 6:40   2381    8/9/2018 7:26   5185    26.42   46.73333333 Ready   8   Production
8/9/2018 0:00   Day Shift   EX04    8/9/2018 7:26   5185    8/9/2018 7:49   6520    22.25   22.25   Utility 8   Short Move 10 Mins
8/9/2018 0:00   Day Shift   EX04    8/9/2018 7:49   6520    8/9/2018 8:11   7875    11.33333333 22.58333333 Standby 8   Accident

任何帮助将不胜感激

0 个答案:

没有答案