按小时计算总和&计算百分比效率

时间:2017-07-15 06:58:35

标签: sql sql-server time

新手SQL。我试图找出一种方法,总结一下,在一段时间内按小时计算机器包装的货物数量。

我有两张桌子:

  1. 会话表 - 存储作业开始&结束时间
  2. 记录表 - 存储每5或10分钟由用户记录的机器包装数量的数据
  3. 会话表

    SES_ID   SES_NOTE         SES_START           SES_END
      1     A000000001  2017-04-21 07:10:20  2017-04-21 15:10:10
      2     A000000078  2017-04-21 15:20:33  2017-04-21 23:10:25
    

    样本记录表:

           TIME           REC_NOTE     SKU        TOTAL    SES_NOTE
    2017-04-21 07:10:20  A000000001  ABC1234567    90     A000000001
    2017-04-21 07:15:11  A000000002  ABC1234567    97     A000000001
    2017-04-21 07:20:33  A000000003  ABC1234567    80     A000000001
    2017-04-21 07:30:06  A000000004  ABC1234567    111    A000000001
    2017-04-21 07:40:23  A000000005  ABC1234567    120    A000000001
    2017-04-21 07:45:44  A000000006  ABC1234567    89     A000000001
    2017-04-21 07:50:18  A000000007  ABC1234567    92     A000000001
    2017-04-21 08:00:15  A000000008  ABC1234567    87     A000000001
    2017-04-21 08:05:01  A000000009  ABC1234567    95     A000000001
    2017-04-21 08:10:22  A000000010  ABC1234567    99     A000000001
    2017-04-24 08:20:38  A000000011  ABC1234567    126    A000000001
    2017-04-21 08:30:50  A000000012  ABC1234568    118    A000000001
    2017-04-21 08:40:27  A000000013  ABC1234567    130    A000000001
    2017-04-21 08:45:48  A000000014  ABC1234567    89     A000000001
    2017-04-21 08:50:36  A000000015  ABC1234567    99     A000000001
    2017-04-21 09:00:58  A000000016  ABC1234567    98     A000000001
    2017-04-21 09:05:54  A000000017  ABC1234567    89     A000000001
    2017-04-21 09:10:12  A000000018  ABC1234567    95     A000000001
    2017-04-21 09:20:29  A000000019  ABC1234567    99     A000000001
    2017-04-21 09:25:25  A000000020  ABC1234567    99     A000000001
    2017-04-21 09:30:29  A000000021  ABC1234567    100    A000000001
    2017-04-21 09:40:45  A000000022  ABC1234567    130    A000000001
    2017-04-21 09:50:31  A000000023  ABC1234567    120    A000000001
    2017-04-21 10:00:02  A000000024  ABC1234567    110    A000000001
    2017-04-21 10:05:05  A000000025  ABC1234567    111    A000000001
    2017-04-21 10:10:23  A000000026  ABC1234567    77     A000000001
    2017-04-21 10:15:56  A000000027  ABC1234567    69     A000000001
    2017-04-21 10:20:01  A000000028  ABC1234567    80     A000000001
    2017-04-21 10:25:02  A000000029  ABC1234567    85     A000000001
    2017-04-21 10:30:07  A000000030  ABC1234567    89     A000000001
    2017-04-21 10:35:09  A000000031  ABC1234567    80     A000000001
    2017-04-21 10:40:13  A000000032  ABC1234567    60     A000000001
    2017-04-21 10:50:41  A000000033  ABC1234567    120    A000000001
    2017-04-21 10:55:48  A000000034  ABC1234567    59     A000000001
    2017-04-21 11:00:10  A000000035  ABC1234567    78     A000000001
    2017-04-21 11:05:23  A000000036  ABC1234567    82     A000000001
    2017-04-21 11:10:11  A000000037  ABC1234567    99     A000000001
    2017-04-21 11:20:26  A000000038  ABC1234567    119    A000000001
    2017-04-21 11:25:33  A000000039  ABC1234567    75     A000000001
    2017-04-21 11:30:56  A000000040  ABC1234567    76     A000000001
    2017-04-21 11:35:43  A000000041  ABC1234567    67     A000000001
    2017-04-21 11:40:24  A000000042  ABC1234567    84     A000000001
    2017-04-21 11:50:12  A000000043  ABC1234567    99     A000000001
    2017-04-21 11:55:28  A000000044  ABC1234567    74     A000000001
    2017-04-21 12:00:01  A000000045  ABC1234568    80     A000000001
    2017-04-21 12:05:04  A000000046  ABC1234568    77     A000000001
    2017-04-21 12:10:17  A000000047  ABC1234568    90     A000000001
    2017-04-21 12:20:41  A000000048  ABC1234568    89     A000000001
    2017-04-21 12:25:11  A000000049  ABC1234568    88     A000000001
    2017-04-21 12:30:54  A000000050  ABC1234567    90     A000000001
    2017-04-21 12:35:31  A000000051  ABC1234567    79     A000000001
    2017-04-21 12:40:21  A000000052  ABC1234567    90     A000000001
    2017-04-21 12:50:14  A000000053  ABC1234567    80     A000000001
    2017-04-21 12:55:09  A000000054  MB145103632   90     A000000001
    2017-04-21 13:00:02  A000000055  ABC1234567    93     A000000001
    2017-04-21 13:05:08  A000000056  ABC1234567    90     A000000001
    2017-04-21 13:10:50  A000000057  ABC1234567    70     A000000001
    2017-04-21 13:20:44  A000000058  ABC1234567    65     A000000001
    2017-04-21 13:25:37  A000000059  ABC1234567    90     A000000001
    2017-04-21 13:30:12  A000000060  ABC1234567    89     A000000001
    2017-04-21 13:35:23  A000000061  ABC1234567    70     A000000001
    2017-04-21 13:40:41  A000000062  ABC1234567    78     A000000001
    2017-04-21 13:50:13  A000000063  ABC1234567    120    A000000001
    2017-04-21 13:55:13  A000000064  ABC1234567    85     A000000001
    2017-04-21 14:00:09  A000000065  ABC1234567    77     A000000001
    2017-04-21 14:05:00  A000000066  ABC1234567    86     A000000001
    2017-04-21 14:10:49  A000000067  ABC1234567    80     A000000001
    2017-04-21 14:15:18  A000000068  ABC1234567    91     A000000001
    2017-04-21 14:20:13  A000000069  ABC1234568    65     A000000001
    2017-04-21 14:25:19  A000000070  ABC1234568    80     A000000001
    2017-04-21 14:30:48  A000000071  ABC1234568    80     A000000001
    2017-04-21 14:35:37  A000000072  ABC1234568    71     A000000001
    2017-04-21 14:40:44  A000000073  ABC1234567    75     A000000001
    2017-04-21 14:50:51  A000000074  ABC1234568    77     A000000001
    2017-04-21 14:55:27  A000000075  ABC1234568    76     A000000001
    2017-04-21 15:00:35  A000000076  ABC1234567    69     A000000001
    2017-04-21 15:10:10  A000000077  ABC1234568    79     A000000001
    2017-04-21 15:20:33  A000000078  ABC1234567    99     A000000078
    2017-04-21 15:25:06  A000000079  ABC1234568    80     A000000078
    2017-04-21 15:30:23  A000000080  ABC1234568    82     A000000078
    2017-04-21 15:35:44  A000000081  ABC1234567    70     A000000078
    2017-04-21 15:40:18  A000000082  ABC1234568    60     A000000078
    2017-04-21 15:50:15  A000000083  ABC1234567    99     A000000078
    2017-04-21 15:55:01  A000000084  ABC1234567    111    A000000078
    2017-04-21 16:00:22  A000000085  ABC1234567    74     A000000078
    2017-04-24 16:05:38  A000000086  ABC1234567    81     A000000078
    2017-04-21 16:10:50  A000000087  ABC1234567    85     A000000078
    2017-04-21 16:20:27  A000000088  ABC1234567    84     A000000078
    2017-04-21 16:30:48  A000000089  ABC1234567    136    A000000078
    2017-04-21 16:40:36  A000000090  ABC1234567    114    A000000078
    2017-04-21 16:45:58  A000000091  ABC1234567    106    A000000078
    2017-04-21 16:50:54  A000000092  10304201      83     A000000078
    2017-04-21 16:55:12  A000000093  10304201      60     A000000078
    2017-04-21 17:00:29  A000000094  10304201      89     A000000078
    2017-04-21 17:05:25  A000000095  10304201      99     A000000078
    2017-04-21 17:10:29  A000000096  10304201      99     A000000078
    2017-04-21 17:20:45  A000000097  10304201      73     A000000078
    2017-04-21 17:30:31  A000000098  10304202A     120    A000000078
    2017-04-21 17:35:02  A000000099  18312101      125    A000000078
    2017-04-21 17:40:05  A000000100  18314402      75     A000000078
    2017-04-21 17:45:23  A000000101  18312101      60     A000000078
    2017-04-21 17:50:56  A000000102  10304202T     76     A000000078
    2017-04-21 17:55:01  A000000103  10304201      64     A000000078
    2017-04-21 18:00:02  A000000104  10304201      79     A000000078
    2017-04-21 18:05:07  A000000105  10304201      80     A000000078
    2017-04-21 18:10:25  A000000106  10304201      75     A000000078
    2017-04-21 18:20:29  A000000107  10304201      70     A000000078
    2017-04-21 18:25:45  A000000108  10304202T     89     A000000078
    2017-04-21 18:30:31  A000000109  ABC1234567    79     A000000078
    2017-04-21 18:35:02  A000000110  ABC1234567    89     A000000078
    2017-04-21 18:40:05  A000000111  ABC1234567    68     A000000078
    2017-04-21 18:50:23  A000000112  ABC1234567    70     A000000078
    2017-04-21 18:55:56  A000000113  ABC1234567    130    A000000078
    2017-04-21 19:00:01  A000000114  ABC1234567    77     A000000078
    2017-04-21 19:05:07  A000000115  ABC1234567    60     A000000078
    2017-04-21 19:10:25  A000000116  ABC1234567    111    A000000078
    2017-04-21 19:20:29  A000000117  ABC1234567    85     A000000078
    2017-04-21 19:25:45  A000000118  ABC1234567    84     A000000078
    2017-04-21 19:30:31  A000000119  ABC1234567    73     A000000078
    2017-04-21 19:35:02  A000000120  ABC1234567    80     A000000078
    2017-04-21 19:40:05  A000000121  ABC1234567    69     A000000078
    2017-04-21 19:50:23  A000000122  ABC1234567    70     A000000078
    2017-04-21 19:55:56  A000000123  ABC1234567    120    A000000078
    2017-04-21 20:00:01  A000000124  ABC1234567    77     A000000078
    2017-04-21 20:05:07  A000000125  ABC1234567    83     A000000078
    2017-04-21 20:10:25  A000000126  ABC1234567    117    A000000078
    2017-04-21 20:20:29  A000000127  ABC1234567    83     A000000078
    2017-04-21 20:25:45  A000000128  ABC1234567    79     A000000078
    2017-04-21 20:30:31  A000000129  ABC1234567    66     A000000078
    2017-04-21 20:35:02  A000000130  ABC1234567    65     A000000078
    2017-04-21 20:40:05  A000000131  ABC1234567    80     A000000078
    2017-04-21 20:50:23  A000000132  ABC1234567    74     A000000078
    2017-04-21 20:55:56  A000000133  ABC1234567    120    A000000078
    2017-04-21 21:00:01  A000000134  ABC1234567    87     A000000078
    2017-04-21 21:05:07  A000000135  ABC1234567    81     A000000078
    2017-04-21 21:10:25  A000000136  ABC1234567    130    A000000078
    2017-04-21 21:20:29  A000000137  ABC1234567    78     A000000078
    2017-04-21 21:25:45  A000000138  ABC1234567    85     A000000078
    2017-04-21 21:30:31  A000000139  ABC1234567    64     A000000078
    2017-04-21 21:35:02  A000000140  ABC1234567    76     A000000078
    2017-04-21 21:40:05  A000000141  ABC1234567    86     A000000078
    2017-04-21 21:50:23  A000000142  ABC1234567    76     A000000078
    2017-04-21 21:55:56  A000000143  ABC1234567    135    A000000078
    2017-04-21 22:00:01  A000000144  ABC1234567    66     A000000078
    2017-04-21 22:05:07  A000000145  ABC1234567    50     A000000078
    2017-04-21 22:10:25  A000000146  ABC1234567    126    A000000078
    2017-04-21 22:20:29  A000000147  ABC1234567    88     A000000078
    2017-04-21 22:25:45  A000000148  ABC1234567    68     A000000078
    2017-04-21 22:30:31  A000000149  ABC1234567    79     A000000078
    2017-04-21 22:35:02  A000000150  ABC1234567    89     A000000078
    2017-04-21 22:40:05  A000000151  ABC1234567    85     A000000078
    2017-04-21 22:50:23  A000000152  ABC1234567    77     A000000078
    2017-04-21 22:55:56  A000000153  ABC1234567    90     A000000078
    2017-04-21 23:00:01  A000000154  ABC1234567    87     A000000078
    2017-04-21 23:05:07  A000000155  ABC1234567    74     A000000078
    2017-04-21 23:10:25  A000000156  ABC1234567    121    A000000078 
    

    从样本表中,我想确定:

    1. 每小时总计TOTAL。
    2. 包装机每小时的效率。假设机器每小时可以包装的产品是880,效率将是=(每小时总数的总和)* 100/880
    3. 结果将如下格式:

           Time         Total  Mach_Speed  Efficiency (%)
      2017-04-21 07:00   960      880          109.09
      

      感谢任何人都可以提供帮助。

1 个答案:

答案 0 :(得分:0)

未经测试的SQL Server 2012语法:

SELECT      DATETIMEFROMPARTS(YEAR(TIME), MONTH(TIME), DAY(TIME), DATEPART(HOUR, TIME), 0, 0, 0)    AS [Hour]
        ,   SUM(TOTAL)                                                                              AS [Total]
FROM        Records
GROUP BY    DATETIMEFROMPARTS(YEAR(TIME), MONTH(TIME), DAY(TIME), DATEPART(HOUR, TIME), 0, 0, 0)