MySQL - 返回两个事件之间的记录总和

时间:2014-11-20 01:48:23

标签: mysql

好的,我不知道如何解释这个问题,这是我的第一篇文章。我是MySQL的相对noobie,过去两周的搜索让我这么远。现在我被卡住了。

我有一张桌子,包含大约120个不同方面的电力和温度场。此表每分钟插入一条新记录(每天1440条记录)。其中一个字段是时间戳。

一个字段在不常见且不可预测的基础上更新(抄表) 我想结合这里包含的两个查询的特征(第一个按天返回kWh,第二个返回仪表读数和仪表读数的日期时间)以返回仪表读数之间测量的总kWh。

我似乎无法弄清楚两件事:

如何执行使用动态日期作为间隔的“分组依据”语句?

如何从每个仪表读取并实际利用日期时间并将其解析为“datetime from”和“datetime to”以向第二个查询提供动态日期时间间隔(我假设我需要查询和子在这里查询)。

也许这些例子有助于解释:

SELECT DATE(`datetime`),
   SUM(powerdata2.CH1WATT)/1000 / 60 + 
   SUM(powerdata2.CH2WATT)/1000 / 60 +
   SUM(powerdata2.CH3WATT) /1000/ 60 +
   SUM(powerdata2.CH4WATT)/1000/ 60 +
   SUM(powerdata2.CH5WATT)/1000 / 60 +
   SUM(powerdata2.CH6WATT)/1000 / 60 +
   SUM(powerdata2.CH7WATT)/1000/ 60 +
   SUM(powerdata2.CH8WATT)/1000/ 60 +
   SUM(powerdata2.CH9WATT)/1000/ 60 +
   SUM(powerdata2.CH10WATT)/1000/ 60 +
   SUM(powerdata2.CH11WATT)/1000/ 60 +
   SUM(powerdata2.CH12WATT)/1000/ 60 +
   SUM(powerdata2.CH13WATT)/1000/ 60 +
   SUM(powerdata2.CH14WATT)/1000/ 60 +
   SUM(powerdata2.CH15WATT)/1000/ 60 +
   SUM(powerdata2.CH16WATT)/1000/ 60 +
   SUM(powerdata2.CH17WATT)/1000/ 60 +
   SUM(powerdata2.CH18WATT)/1000/ 60 +
   SUM(powerdata2.CH19WATT)/1000/ 60 +
   SUM(powerdata2.CH20WATT)/1000/ 60 +
   SUM(powerdata2.CH21WATT)/1000/ 60 +
   SUM(powerdata2.CH22WATT)/1000/ 60 +
   SUM(powerdata2.CH23WATT)/1000/ 60 +
   SUM(powerdata2.CH24WATT)/1000/ 60 +
   SUM(powerdata2.CH25WATT)/1000/ 60 +
   SUM(powerdata2.CH26WATT)/1000/ 60 +
   SUM(powerdata2.CH27WATT)/1000/ 60 +
   SUM(powerdata2.CH28WATT)/1000/ 60 +
   SUM(powerdata2.CH29WATT)/1000/ 60 +
   SUM(powerdata2.CH30WATT)/1000/ 60 +
   SUM(powerdata2.CH31WATT)/1000/ 60 +
   SUM(powerdata2.CH32WATT)/1000/ 60 AS BYLOADTOTAL,
   SUM(powerdata2.CH33WATT)/1000/ 60 +
   SUM(powerdata2.CH34WATT)/1000/ 60 AS BYLINETOTAL

FROM db_pdata.powerdata2 powerdata2
GROUP BY DATE(`datetime`)


DATE(`datetime`)    BYLOADTOTAL BYLINETOTAL
11/16/2014 12:00:00 AM  83.525883462254 91.148740563456
11/17/2014 12:00:00 AM  70.079734716742 76.287172546895
11/18/2014 12:00:00 AM  73.810085604727 79.983184481812
11/19/2014 12:00:00 AM  74.147556015524 80.981706193542

查询#2:

SELECT powerdata2.`datetime`, powerdata2.MeterReading
FROM db_pdata.powerdata2 powerdata2
WHERE powerdata2.MeterReading <> 0
GROUP BY powerdata2.MeterReading



datetime    MeterReading
11/16/2014 11:15:42 AM  31665
11/17/2014 8:08:59 PM   31774
11/18/2014 8:56:36 PM   31845
11/19/2014 6:19:36 PM   31919

以下查询显示表的整个数据结构:

SELECT powerdata2.*
FROM db_pdata.powerdata2 powerdata2
LIMIT 10


datetime    MeterReading    Frequency   LINEVOLTS   CH1AMP  CH1WATT CH1VA   CH2AMP  CH2WATT CH2VA   CH3AMP  CH3WATT CH3VA   CH4VA   CH4AMP  CH4WATT CH5AMP  CH5WATT CH5VA   CH6AMP  CH6WATT CH6VA   CH7AMP  CH7WATT CH7VA   CH8AMP  CH8WATT CH8VA   CH9AMP  CH9WATT CH9VA   CH10AMP CH10WATT    CH10VA  CH11AMP CH11WATT    CH11VA  CH12VA  CH12AMP CH12WATT    CH13AMP CH13WATT    CH13VA  CH14AMP CH14WATT    CH14VA  CH15AMP CH15WATT    CH15VA  CH16AMP CH16WATT    CH16VA  CH17AMP CH17WATT    CH17VA  CH18AMP CH18WATT    CH18VA  CH19AMP CH19WATT    CH19VA  CH20AMP CH20WATT    CH20VA  CH21AMP CH21WATT    CH21VA  CH22AMP CH22WATT    CH22VA  CH23AMP CH23WATT    CH23VA  CH24AMP CH24WATT    CH24VA  CH25AMP CH25WATT    CH25VA  CH26AMP CH26WATT    CH26VA  CH27AMP CH27WATT    CH27VA  CH28AMP CH28WATT    CH28VA  CH29AMP CH29WATT    CH29VA  CH30AMP CH30WATT    CH30VA  CH31AMP CH31WATT    CH31VA  CH32AMP CH32WATT    CH32VA  CH33AMP CH33WATT    CH33VA  CH34AMP CH34WATT    CH34VA  Temp1   Temp2   Temp3   Temp4
11/16/2014 12:59:57 AM  0   59.9975 120.1   0   0   0   0   0   0   2.887   318.3   349.1   93.24   0.7727  66.8    2.042   238.7   245.2   0.982   81.06   118.7   0   0   0   9.78    974.8   1173    0   0   0   0.2342  23.43   28.17   4.939   1160    1193    54.53   1.942   173.9   5   510.2   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.1943  14.35   23.33   0.4549  53.92   0   0   0   0   0   0   0   0   0   0   21.11   2472    2531    12.2    1419    1466                
11/16/2014 1:00:42 AM   0   59.9975 120.495 0   0   0   0   0   0   2.82424 303.538 340.824 95.1209 0.788176    69.2719 2.04481 239.852 246.324 1.01697 85.4419 122.629 0   0   0   1.11283 109.571 133.478 0   0   0   0.246643    24.5752 29.7424 5.172   1210.29 1247.76 54.7643 1.59219 148.538 4.95662 505.867 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0055619   0.613333    0.669524    0.185081    15.6624 22.3086 0.454357    54.1752 0   0   0   0   0   0   0   0   0   0   12.6476 1487.81 1525.05 12.2433 1432.24 1476                
11/16/2014 1:01:42 AM   0   59.9965 120.56  0   0   0   0   0   0   2.81705 303.66  340.035 92.0565 0.762805    67.109  2.0443  239.825 246.2   1.02508 85.6915 123.46  0.003385    0.3083  0.40885 0.204645    18.7815 24.6745 0   0   0   0.24485 24.357  29.542  5.2875  1232.8  1273.55 54.726  1.13945 114.38  5.14215 529.75  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.01213 1.34    1.464   0.18982 16.088  22.89   0.453995    54.105  0   0   0   0   0   0   0   0   0   0   11.9675 1415.35 1444.15 12.5765 1467.55 1517.15             
11/16/2014 1:02:42 AM   0   60.0238 120.581 0   0   0   0   0   0   2.8039  299.705 338.276 93.9919 0.778495    68.4267 2.04248 239.957 246.262 1.0139  84.7433 122.2   0   0   0   2.03708 203.555 245.21  0   0   0   0.245352    24.3976 29.591  5.39748 1261.48 1300.86 54.5971 1.11214 113.424 5.33819 558.21  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0218048   2.37286 2.63619 0.185895    15.5419 22.4381 0.452962    53.9957 0   0   0   0   0   0   0   0   0   0   13.2929 1566.9  1601.19 12.9143 1505.24 1558.05             
11/16/2014 1:03:42 AM   0   60.0927 120.66  0   0   0   0   0   0   2.84455 302.485 343.03  92.8405 0.76965 66.9745 2.0453  240.535 246.835 1.00861 84.7535 121.66  0   0   0   0.20344 18.742  24.536  0   0   0   0.24779 24.842  29.9135 5.3822  1261.2  1298.45 54.701  1.14695 115.275 5.33315 556.775 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.01747 1.8975  2.111   0.187335    16.171  22.6255 0.453605    54.1005 0   0   0   0   0   0   0   0   0   0   12.0315 1422.5  1451.05 12.8825 1504.9  1555.5              
11/16/2014 1:04:42 AM   0   60.0518 120.671 0   0   0   0   0   0   2.798   298.557 337.976 92.8676 0.768733    67.1881 2.0439  240.195 246.743 1.02934 86.6429 124.271 0   0   0   0.204162    18.8095 24.6671 0   0   0   0.246281    24.5333 29.7543 5.29024 1236.67 1277.9  54.9748 1.13286 114.6   5.30814 555.69  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0123143   1.37524 1.49048 0.189557    16.0786 22.8952 0.455343    54.3838 0   0   0   0   0   0   0   0   0   0   11.9071 1409.24 1438.71 12.7562 1487.57 1540.1              
11/16/2014 1:05:42 AM   0   60.0268 120.63  0   0   0   0   0   0   2.82135 302.145 340.595 92.497  0.766315    66.8505 2.0428  240.36  246.71  1.02587 86.241  123.79  0.006635    0.6339  0.80335 0.460515    49.147  55.6065 0   0   0   0.244355    24.314  29.491  5.0421  1176.2  1216.85 54.6975 1.1284  114 5.12105 529.675 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.005605    0.597   0.674   0.187055    15.8315 22.587  0.45323 54.092  0   0   0   0   0   0   0   0   0   0   11.989  1419.25 1447.8  12.243  1432.4  1477.75             
11/16/2014 1:08:15 AM   0   59.9694 120.291 0   0   0   0   0   0   2.82627 296.709 339.182 92.3236 0.765864    66.9227 2.03591 239.173 245.282 1.04531 87.4973 125.791 0   0   0   1.12709 127.345 135.664 0   0   0   0.247509    24.42   29.7845 4.75882 1102.64 1144.55 54.4818 2.08109 183.764 4.87545 494.482 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0108273   1.13727 1.29636 0.189227    16.6055 22.7927 0.452664    53.78   0   0   0   0   0   0   0   0   0   0   13.0745 1535.18 1572.18 11.6382 1359.27 1400.64             
11/16/2014 1:09:15 AM   0   59.9694 119.987 0   0   0   0   0   0   2.816   295.713 338.393 92.6993 0.771933    66.5873 2.0314  238.267 244.28  1.04151 86.9147 125.127 0   0   0   1.12573 127.027 135.42  0   0   0   0.247393    24.438  29.7687 4.93753 1147.73 1187.93 54.2427 2.08    183.833 4.86893 494.36  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.185   15.802  22.2093 0.45134 53.5273 0   0   0   0   0   0   0   0   0   0   13.196  1548.87 1585.93 11.766  1369.2  1412.47             
11/16/2014 1:11:42 AM   0   59.9902 120.533 0   0   0   0   0   0   2.87327 309.427 346.127 92.2993 0.766307    66.8533 2.03687 239.433 245.633 1.01329 84.8533 122.107 0   0   0   1.11633 126.267 134.68  0   0   0   0.2454  24.4787 29.5713 5.14507 1205.67 1241.67 54.3293 2.04667 182.04  4.87827 495.713 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.180413    15.566  21.7293 0.450887    53.7473 0   0   0   0   0   0   0   0   0   0   13.4787 1586.67 1623.13 12.0447 1407.93 1452.73             

0 个答案:

没有答案