SUM持续时间的原因

时间:2017-12-14 09:12:10

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

在矿山的车队管理系统中,我们有办法跟踪挖掘机在特定状态下花费的时间。我现在需要计算每个州花费的时间。我的查询和表格如下。我需要加入3个不同的表来获取日期,原因,持续时间和原因代码才能使这项工作成功。我似乎无法将个别理由的时间总结,它会继续分裂时间。

select

       hs.shiftindex
      ,hs.reason
      ,hs.status
      ,sum(duration/60) as minutes
      ,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 = '2017-12-13'
and eqmt in ('ex02','ex03','ex04','ex05','ex06','ex07') 
and hs.status <>2
and hs.status <>1
and hr.name not like ('SHIFT CHANGE')
and hs.reason in ('7005','5010','6615','6705')
group by hs.shiftindex, duration, hs.reason, hs.status, hr.name
order by duration desc

表: hist_statusevents:

shiftindex  cliID   ddbkey  eqmt    unit    operid  starttime   endtime duration    reason  status  category    comment vEvent  reasonlink  stdcomm
52139   31172   2374056 EX02    2   mmsunk  0   1406    1406    6011    4   3       0   2303324 Other
52139   31172   2374246 EX02    2   001314  1406    1872    466 6011    4   3       0   2303324 Other
52139   31172   2374436 EX02    2   001314  1872    4402    2530    7000    2   1       1   2303006 Other
52139   31172   2374626 EX02    2   001314  4402    4650    248 6028    4   3       1   2373950 Other
52139   31172   2374816 EX02    2   001314  4650    5583    933 7000    2   1       1   2303006 Other
52139   31172   2375006 EX02    2   001314  5583    6059    476 6031    4   3       1   2372134 Other
52139   31172   2375196 EX02    2   001314  6059    17100   11041   7000    2   1       1   2303006 Other
52139   31172   2375386 EX02    2   001314  17100   17791   691 5124    3   4       1   2306302 Other
52139   31172   2375576 EX02    2   001314  17791   21701   3910    7000    2   1       1   2303006 Other
52139   31172   2375766 EX02    2   001314  21701   22056   355 6028    4   3       1   2373950 Other
52139   31172   2375956 EX02    2   001314  22056   22987   931 7000    2   1       1   2303006 Other
52139   31172   2376146 EX02    2   001314  22987   24119   1132    5010    4   2       1   417238  Other
52139   31172   2376336 EX02    2   001314  24119   28800   4681    7000    2   1       1   2303006 Other
52139   31172   2376526 EX03    2   mmsunk  0   734 734 7000    2   1       0   2303006 Other
52139   31172   2376716 EX03    2   mmsunk  734 735 1   5060    4   3       1   2303112 Other
52139   31172   2376906 EX03    2   mmsunk  735 1442    707 5060    4   3       0   2303112 Other
52139   31172   2377096 EX03    2   001311  1442    1460    18  5060    4   3       0   2303112 Other
52139   31172   2377286 EX03    2   001311  1460    1468    8   5060    4   3       0   2303112 Other
52139   31172   2377476 EX03    2   001311  1468    2663    1195    6033    3   4       1   2356152 Other
52139   31172   2377666 EX03    2   001311  2663    6943    4280    7000    2   1       1   2303006 Other
52139   31172   2377856 EX03    2   001311  6943    7340    397 6000    4   3       1   2306408 Other
52139   31172   2378046 EX03    2   001311  7340    22911   15571   7000    2   1       1   2303006 Other
52139   31172   2378236 EX03    2   001311  22911   24095   1184    5010    4   2       1   417238  Other
52139   31172   2378426 EX03    2   001311  24095   28800   4705    7000    2   1       1   2303006 Other
52139   31172   2378616 EX04    2   mmsunk  0   28800   28800   3000    1   6   1000 HRS SERVICE    0   2302604 1000 HRS SERVICE
52139   31172   2378806 EX05    2   mmsunk  0   28800   28800   1705    1   5   RING GEAR FAILURE   0   408122  Other
52139   31172   2379102 EX06    2   mmsunk  0   970 970 7000    2   1       0   2303006 Other
52139   31172   2379292 EX06    2   mmsunk  970 1323    353 5010    4   2       1   417238  Other
52139   31172   2379482 EX06    2   mmsunk  1323    1337    14  7000    2   1       1   2303006 Other
52139   31172   2379672 EX06    2   003455  1337    6844    5507    7000    2   1       0   2303006 Other
52139   31172   2379862 EX06    2   003455  6844    7584    740 6034    3   4       1   2378996 Other
52139   31172   2380052 EX06    2   003455  7584    20820   13236   7000    2   1       1   2303006 Other
52139   31172   2380242 EX06    2   003455  20820   21605   785 6040    4   3       1   2308900 Other
52139   31172   2380432 EX06    2   003455  21605   22908   1303    7000    2   1       1   2303006 Other
52139   31172   2380622 EX06    2   003455  22908   24493   1585    5010    4   2       1   417238  Other
52139   31172   2380812 EX06    2   003455  24493   28800   4307    7000    2   1       1   2303006 Other
52139   31172   2381108 EX07    2   mmsunk  0   240 240 7000    2   1       0   2303006 Other
52139   31172   2381298 EX07    2   mmsunk  240 386 146 6033    3   4       1   2356152 Other
52139   31172   2381488 EX07    2   mmsunk  386 1380    994 6033    3   4       0   2356152 Other
52139   31172   2381678 EX07    2   mmsunk  1380    1623    243 5060    4   3       1   2303112 Other
52139   31172   2381868 EX07    2   001479  1623    2098    475 5060    4   3       0   2303112 Other
52139   31172   2382058 EX07    2   001479  2098    3780    1682    7000    2   1       1   2303006 Other
52139   31172   2382248 EX07    2   001479  3780    4379    599 6000    4   3       1   2306408 Other
52164   31172   1987914 EX02    2   mmsunk  0   1038    1038    100 2   1       0   405860  Other
52164   31172   1988104 EX02    2   mmsunk  1038    1487    449 6315    3   4       1   408616  Other
52164   31172   1988294 EX02    2   001196  1487    1906    419 6315    3   4       0   408616  Other
52164   31172   1988484 EX02    2   001196  1906    16191   14285   100 2   1       1   405860  Other
52164   31172   1988674 EX02    2   001196  16191   20599   4408    5105    4   3       1   411372  Other
52164   31172   1988864 EX02    2   001196  20599   20823   224 100 2   1       1   405860  Other
52164   31172   1989054 EX02    2   001196  20823   22041   1218    6905    4   3       1   415082  Other

hist_reasontable:

shiftindex  cliID   ddbkey  name    reason  status  delaytime   category    mainttime   altname center
52139   31172   396992  Control 510 1   1200    6   NO      01. Availability
52139   31172   397098  Motor   515 1   1200    6   NO      01. Availability
52139   31172   397204  Saddle Blocks   520 1   1200    6   NO      01. Availability
52139   31172   397310  Tension System  525 1   1200    6   NO      01. Availability
52139   31172   397416  Transmission    530 1   1200    6   NO      01. Availability
52139   31172   397522  MECHANICAL DRIVE    600 1   1200    6   NO      01. Availability
52139   31172   397628  Controls    610 1   1200    6   NO      01. Availability
52139   31172   397734  Differential    615 1   1200    6   NO      01. Availability
52139   31172   397840  Dynamics Retarder   620 1   1200    6   NO      01. Availability
52139   31172   397946  Final Drive 625 1   1200    6   NO      01. Availability
52139   31172   398052  Tandem Drive    630 1   1200    6   NO      01. Availability
52139   31172   398158  Torque Converter    635 1   1200    6   NO      01. Availability
52139   31172   398264  ELECTRICAL-PROPULSION   640 1   1200    6   NO      01. Availability
52139   31172   398370  TRANSMISSION    645 1   1200    6   NO      01. Availability
52139   31172   398476  Wheel Motor 650 1   1200    6   NO      01. Availability
52139   31172   398582  ELECTRICAL  700 1   1200    6   NO      01. Availability
52139   31172   398688  Control 705 1   1200    6   NO      01. Availability
52139   31172   398794  Lighting    710 1   1200    6   NO      01. Availability
52139   31172   398900  Motor   715 1   1200    6   NO      01. Availability
52139   31172   399006  Transformer 720 1   1200    6   NO      01. Availability
52139   31172   399112  Electrical 12/24 Volts  800 1   1200    6   NO      01. Availability
52139   31172   399218  Alt 805 1   1200    6   NO      01. Availability
52139   31172   399324  Battery 810 1   1200    6   NO      01. Availability
52139   31172   399430  Control 815 1   1200    6   NO      01. Availability
52139   31172   399536  ELECTRICAL-LIGHTING 820 1   1200    6   NO      01. Availability
52139   31172   399642  Starter 825 1   1200    6   NO      01. Availability
52139   31172   399748  Wiring  830 1   1200    6   NO      01. Availability
52139   31172   399854  ELECTRICAL-POWER FAILURE    900 1   1200    6   NO      01. Availability
52139   31172   399960  Reel / Stinger  905 1   1200    6   NO      01. Availability
52139   31172   400066  Switch Gear 910 1   1200    6   NO      01. Availability
52139   31172   400172  Transformer 915 1   1200    6   NO      01. Availability
52139   31172   400278  Aux Transformer 920 1   1200    6   NO      01. Availability
52139   31172   400384  Centurion System    925 1   1200    6   NO      01. Availability
52139   31172   400490  Collector   930 1   1200    6   NO      01. Availability
52139   31172   400596  Converter/Inverter  935 1   1200    6   NO      01. Availability
52139   31172   400702  Load Centers    940 1   1200    6   NO      01. Availability
52139   31172   400808  MCC 945 1   1200    6   NO      01. Availability
52139   31172   400914  ELECTRICAL-ZESCO EXTERNAL   950 1   1200    6   NO      01. Availability
52139   31172   401020  Reactive Power  955 1   1200    6   NO      01. Availability
52139   31172   401126  Transformer 960 1   1200    6   NO      01. Availability
52139   31172   401232  ENGINE  1000    1   1200    6   NO      01. Availability
52139   31172   401338  Controls    1005    1   1200    6   NO      01. Availability
52139   31172   401444  ENGINE-COOLING SYSTEM   1010    1   1200    6   NO      01. Availability
52139   31172   401550  Complete Engine 1015    1   1200    6   NO      01. Availability
52139   31172   401656  Exhaust 1020    1   1200    6   NO      01. Availability
52139   31172   401762  OTHER-FUEL TANK 1025    1   1200    6   NO      01. Availability
52139   31172   401868  Intake  1030    1   1200    6   NO      01. Availability
52139   31172   401974  Lubes   1035    1   1200    6   NO      01. Availability
52139   31172   402080  Hoist   1100    1   1200    6   NO      01. Availability
52139   31172   402186  Brake   1105    1   1200    6   NO      01. Availability

hist_exproot:

shiftdate   cliID   ddbkey  shiftindex  name    year    month#  month   day shift#  shift   date    start   crew#   crew    len disptime    holiday ddmmyy  finyear finquarter  finmonth    finweek
2017-09-15 00:00:00 31172   0   52274   15-SEP-17 Night Shift   17  9   SEP 15  3   Night Shift 1505512800  79200   3   Crew C  28800   28800   0   15-09-17    2017    3   9   37
2017-09-19 00:00:00 31172   0   52285   19-SEP-17 Aft Shift 17  9   SEP 19  2   Aft Shift   1505829600  50400   1   Crew A  28800   28800   0   19-09-17    2017    3   9   38
2017-10-21 00:00:00 31172   0   52382   21-OCT-17 Night Shift   17  10  OCT 21  3   Night Shift 1508623200  79200   4   Crew D  28800   28800   0   21-10-17    2017    4   10  43
2017-09-11 00:00:00 31172   0   52261   11-SEP-17 Aft Shift 17  9   SEP 11  2   Aft Shift   1505138400  50400   3   Crew C  28800   28800   0   11-09-17    2017    3   9   37
2017-09-01 00:00:00 31172   0   52232   01-SEP-17 Night Shift   17  9   SEP 1   3   Night Shift 1504303200  79200   2   Crew B  28800   28800   0   01-09-17    2017    3   9   35
2017-09-13 00:00:00 31172   0   52266   13-SEP-17 Day Shift 17  9   SEP 13  1   Day Shift   1505282400  21600   1   Crew A  28800   28800   0   13-09-17    2017    3   9   37
2017-10-24 00:00:00 31172   0   52391   24-OCT-17 Night Shift   17  10  OCT 24  3   Night Shift 1508882400  79200   3   Crew C  28800   28800   0   24-10-17    2017    4   10  43
2017-08-31 00:00:00 31172   0   52227   31-AUG-17 Day Shift 17  8   AUG 31  1   Day Shift   1504159200  21600   4   Crew D  28800   28800   0   31-08-17    2017    3   8   35
2017-10-23 00:00:00 31172   0   52386   23-OCT-17 Day Shift 17  10  OCT 23  1   Day Shift   1508738400  21600   1   Crew A  28800   28800   0   23-10-17    2017    4   10  43
2017-08-29 00:00:00 31172   0   52221   29-AUG-17 Day Shift 17  8   AUG 29  1   Day Shift   1503986400  21600   4   Crew D  28800   28800   0   29-08-17    2017    3   8   35
2017-08-30 00:00:00 31172   0   52226   30-AUG-17 Night Shift   17  8   AUG 30  3   Night Shift 1504130400  79200   2   Crew B  28800   28800   0   30-08-17    2017    3   8   35
2017-09-16 00:00:00 31172   0   52276   16-SEP-17 Aft Shift 17  9   SEP 16  2   Aft Shift   1505570400  50400   2   Crew B  28800   28800   0   16-09-17    2017    3   9   38
2017-09-15 00:00:00 31172   0   52272   15-SEP-17 Day Shift 17  9   SEP 15  1   Day Shift   1505455200  21600   1   Crew A  28800   28800   0   15-09-17    2017    3   9   37
2017-09-05 00:00:00 31172   0   52244   05-SEP-17 Night Shift   17  9   SEP 5   3   Night Shift 1504648800  79200   1   Crew A  28800   28800   0   05-09-17    2017    3   9   36
2017-09-07 00:00:00 31172   0   52248   07-SEP-17 Day Shift 17  9   SEP 7   1   Day Shift   1504764000  21600   3   Crew C  28800   28800   0   07-09-17    2017    3   9   36
2017-09-04 00:00:00 31172   0   52240   04-SEP-17 Aft Shift 17  9   SEP 4   2   Aft Shift   1504533600  50400   4   Crew D  28800   28800   0   04-09-17    2017    3   9   36
2017-09-02 00:00:00 31172   0   52234   02-SEP-17 Aft Shift 17  9   SEP 2   2   Aft Shift   1504360800  50400   1   Crew A  28800   28800   0   02-09-17    2017    3   9   36
2017-09-02 00:00:00 31172   0   52233   02-SEP-17 Day Shift 17  9   SEP 2   1   Day Shift   1504332000  21600   4   Crew D  28800   28800   0   02-09-17    2017    3   9   35
2017-09-07 00:00:00 31172   0   52250   07-SEP-17 Night Shift   17  9   SEP 7   3   Night Shift 1504821600  79200   1   Crew A  28800   28800   0   07-09-17    2017    3   9   36
2017-09-07 00:00:00 31172   0   52249   07-SEP-17 Aft Shift 17  9   SEP 7   2   Aft Shift   1504792800  50400   4   Crew D  28800   28800   0   07-09-17    2017    3   9   36
2017-09-06 00:00:00 31172   0   52246   06-SEP-17 Aft Shift 17  9   SEP 6   2   Aft Shift   1504706400  50400   4   Crew D  28800   28800   0   06-09-17    2017    3   9   36
2017-11-21 00:00:00 31172   0   52473   21-NOV-17 Day Shift 17  11  NOV 21  1   Day Shift   1511244000  21600   4   Crew D  28800   28800   0   21-11-17    2017    4   11  47
2017-08-29 00:00:00 31172   0   52223   29-AUG-17 Night Shift   17  8   AUG 29  3   Night Shift 1504044000  79200   2   Crew B  28800   28800   0   29-08-17    2017    3   8   35
2017-08-31 00:00:00 31172   0   52229   31-AUG-17 Night Shift   17  8   AUG 31  3   Night Shift 1504216800  79200   2   Crew B  28800   28800   0   31-08-17    2017    3   8   35
2017-09-06 00:00:00 31172   0   52247   06-SEP-17 Night Shift   17  9   SEP 6   3   Night Shift 1504735200  79200   1   Crew A  28800   28800   0   06-09-17    2017    3   9   36
2017-09-02 00:00:00 31172   0   52235   02-SEP-17 Night Shift   17  9   SEP 2   3   Night Shift 1504389600  79200   2   Crew B  28800   28800   0   02-09-17    2017    3   9   36
2017-10-18 00:00:00 31172   0   52372   18-OCT-17 Aft Shift 17  10  OCT 18  2   Aft Shift   1508335200  50400   3   Crew C  28800   28800   0   18-10-17    2017    4   10  42
2017-09-04 00:00:00 31172   0   52239   04-SEP-17 Day Shift 17  9   SEP 4   1   Day Shift   1504504800  21600   3   Crew C  28800   28800   0   04-09-17    2017    3   9   36
2017-09-16 00:00:00 31172   0   52275   16-SEP-17 Day Shift 17  9   SEP 16  1   Day Shift   1505541600  21600   1   Crew A  28800   28800   0   16-09-17    2017    3   9   37
2017-09-16 00:00:00 31172   0   52277   16-SEP-17 Night Shift   17  9   SEP 16  3   Night Shift 1505599200  79200   3   Crew C  28800   28800   0   16-09-17    2017    3   9   38
2017-10-31 00:00:00 31172   0   52411   31-OCT-17 Aft Shift 17  10  OCT 31  2   Aft Shift   1509458400  50400   1   Crew A  28800   28800   0   31-10-17    2017    4   10  44
2017-10-25 00:00:00 31172   0   52393   25-OCT-17 Aft Shift 17  10  OCT 25  2   Aft Shift   1508940000  50400   2   Crew B  28800   28800   0   25-10-17    2017    4   10  43
2017-10-20 00:00:00 31172   0   52377   20-OCT-17 Day Shift 17  10  OCT 20  1   Day Shift   1508479200  21600   2   Crew B  28800   28800   0   20-10-17    2017    4   10  42
2017-09-11 00:00:00 31172   0   52262   11-SEP-17 Night Shift   17  9   SEP 11  3   Night Shift 1505167200  79200   4   Crew D  28800   28800   0   11-09-17    2017    3   9   37
2017-09-14 00:00:00 31172   0   52270   14-SEP-17 Aft Shift 17  9   SEP 14  2   Aft Shift   1505397600  50400   2   Crew B  28800   28800   0   14-09-17    2017    3   9   37
2017-08-02 00:00:00 31172   0   52141   02-AUG-17 Aft Shift 17  8   AUG 2   2   Aft Shift   1501682400  50400   3   Crew C  28800   28800   0   02-08-17    2017    3   8   31
2017-08-01 00:00:00 31172   0   52137   01-AUG-17 Day Shift 17  8   AUG 1   1   Day Shift   1501567200  21600   2   Crew B  28800   28800   0   01-08-17    2017    3   8   31
2017-09-17 00:00:00 31172   0   52278   17-SEP-17 Day Shift 17  9   SEP 17  1   Day Shift   1505628000  21600   1   Crew A  28800   28800   0   17-09-17    2017    3   9   38
2017-07-31 00:00:00 31172   0   52135   31-JUL-17 Aft Shift 17  7   JUL 31  2   Aft Shift   1501509600  50400   3   Crew C  28800   28800   0   31-07-17    2017    3   7   31
2017-09-10 00:00:00 31172   0   52258   10-SEP-17 Aft Shift 17  9   SEP 10  2   Aft Shift   1505052000  50400   3   Crew C  28800   28800   0   10-09-17    2017    3   9   37
2017-09-20 00:00:00 31172   0   52289   20-SEP-17 Night Shift   17  9   SEP 20  3   Night Shift 1505944800  79200   2   Crew B  28800   28800   0   20-09-17    2017    3   9   38
2017-12-03 00:00:00 31172   0   52510   03-DEC-17 Aft Shift 17  12  DEC 3   2   Aft Shift   1512309600  50400   2   Crew B  28800   28800   0   03-12-17    2017    4   12  49
2017-09-10 00:00:00 31172   0   52259   10-SEP-17 Night Shift   17  9   SEP 10  3   Night Shift 1505080800  79200   4   Crew D  28800   28800   0   10-09-17    2017    3   9   37
2017-09-13 00:00:00 31172   0   52268   13-SEP-17 Night Shift   17  9   SEP 13  3   Night Shift 1505340000  79200   3   Crew C  28800   28800   0   13-09-17    2017    3   9   37
2017-09-14 00:00:00 31172   0   52269   14-SEP-17 Day Shift 17  9   SEP 14  1   Day Shift   1505368800  21600   1   Crew A  28800   28800   0   14-09-17    2017    3   9   37
2017-10-21 00:00:00 31172   0   52380   21-OCT-17 Day Shift 17  10  OCT 21  1   Day Shift   1508565600  21600   2   Crew B  28800   28800   0   21-10-17    2017    4   10  42
2017-10-11 00:00:00 31172   0   52352   11-OCT-17 Night Shift   17  10  OCT 11  3   Night Shift 1507759200  79200   2   Crew B  28800   28800   0   11-10-17    2017    4   10  41
2017-09-30 00:00:00 31172   0   52319   30-SEP-17 Night Shift   17  9   SEP 30  3   Night Shift 1506808800  79200   4   Crew D  28800   28800   0   30-09-17    2017    3   9   40
2017-09-27 00:00:00 31172   0   52310   27-SEP-17 Night Shift   17  9   SEP 27  3   Night Shift 1506549600  79200   1   Crew A  28800   28800   0   27-09-17    2017    3   9   39
2017-09-29 00:00:00 31172   0   52314   29-SEP-17 Day Shift 17  9   SEP 29  1   Day Shift   1506664800  21600   2   Crew B  28800   28800   0   29-09-17    2017    3   9   39

我得到的是什么:

reason  reason  status  minutes
Lightning   7005    3   93.31666667
Lightning   7005    3   92.06666667
Lightning   7005    3   70.31666667
Lightning   7005    3   61.3
Clean Dig/Work Area 5010    4   15.4
Waiting on Clean Up 6615    3   14.65
Lightning   7005    3   11.7
Clean Dig/Work Area 5010    4   9.316666667
Clean Dig/Work Area 5010    4   9.116666667
Lightning   7005    3   9.05
Clean Dig/Work Area 5010    4   9
Clean Dig/Work Area 5010    4   7.666666667
Clean Dig/Work Area 5010    4   6.8
Clean Dig/Work Area 5010    4   5.933333333
Restricted Access   6705    4   4.266666667
Clean Dig/Work Area 5010    4   4.15
Waiting on Clean Up 6615    3   3.333333333
Clean Dig/Work Area 5010    4   3
Clean Dig/Work Area 5010    4   2.983333333

我期待的是:

reason  reason  status  minutes
Lightning   7005    3   337.75
Clean Dig/Work Area 5010    4   73.36666667
Waiting on Clean Up 6615    3   17.98333333
Restricted Access   6705    4   4.266666667

所以我基本上想要的是一个夏日,我现在得到的是详细的。非常感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

我认为您需要在select语句中使用Over with Sum

android   
{
  dexOptions
 {
       javaMaxHeapSize "4g" //specify the heap size for the dex process

  }
}

答案 1 :(得分:0)

只需将您的论坛更改为包含您想要摘要的列。 通过添加一些聚合函数稍微改变了您的查询。

根据以上针对状态 reasonname(hr.name)列的示例数据,您在摘要中为每个原​​因提供了相同的数据,因此执行了{ {1}}以避免它们在组中使用,以便获得所需的摘要。如果它们不同,你需要使用窗口函数来选择你需要的东西

max()

答案 2 :(得分:0)

只需将持续时间从group by子句开始,然后按分钟排序:

...
group by hs.shiftindex, hs.reason, hs.status, hr.name
order by minutes desc

结果列应全部 按列聚合。如果按聚合列(持续时间)进行分组,则每个持续时间都会有一个单独的行。