最近30天每小时的MySQL平均值

时间:2020-07-14 13:23:03

标签: mysql sql moving-average

我有一张表,该表每分钟更新一次,我需要计算最近30天的值的每小时平均值。

Timestamp            | SB1_AC_GES_DIFF
2020-07-14 15:13:04     30
2020-07-14 15:12:07     27
...                     ...

我想像这样将结果保存在另一个名为avgTable的表中

Timestamp            | AVG_SB1
15:00                  29
16:00                  32
...                    ...

如果表格每天可以更新一次,这可能是完美的,也许是在12点钟并且日期的日期部分发生更改时。

2 个答案:

答案 0 :(得分:1)

您可以尝试:

2020-07-14 09:31:53.667 test.kexe[9090:10700111] Error creating notification handler for simulator graphics quality override: 3
2020-07-14 09:31:53.670 test.kexe[9090:10700111] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'contentScale is unreasonable (NaN or Inf)'
*** First throw call stack:
(
        0   CoreFoundation                      0x00007fff23e3cf0e __exceptionPreprocess + 350
        1   libobjc.A.dylib                     0x00007fff50ba89b2 objc_exception_throw + 48
        2   CoreFoundation                      0x00007fff23e3cad9 -[NSException raise] + 9
        3   VectorKit                           0x00007fff49c14143 _Z25checkForBogusContentScaled + 115
        4   VectorKit                           0x00007fff49c14252 -[VKMapView initShouldRasterize:inBackground:contentScale:auditToken:] + 62
        5   MapKit                              0x00007fff2782009a -[MKBasicMapView initWithFrame:andGlobe:shouldRasterize:] + 419
        6   MapKit                              0x00007fff27755b85 -[MKMapView _commonInitFromIB:gestureRecognizerHostView:showsAttribution:showsAppleLogo:] + 1331
        7   MapKit                              0x00007fff27756698 -[MKMapView initWithFrame:] + 253
        8   test.kexe                           0x000000010e346dd0 _70616e6765612d6170706c655f74657374_knbridge2 + 64
        9   test.kexe                           0x000000010e33fa8b kfun:com.weather.pangea.apple.AppleViewportTest.test creating view() + 987
        10  test.kexe                           0x000000010e3410a6 kfun:com.weather.pangea.apple.$AppleViewportTest$test$0.$test creating view$FUNCTION_REFERENCE$1.invoke#internal + 70
        11  test.kexe                           0x000000010e34111f kfun:com.weather.pangea.apple.$AppleViewportTest$test$0.$test creating view$FUNCTION_REFERENCE$1.$<bridge-UNNN>invoke(P1)#internal + 95
        12  test.kexe                           0x000000010e3e0fa7 kfun:kotlin.native.internal.test.BaseClassSuite.TestCase.run() + 1287
        13  test.kexe                           0x000000010e3d9256 kfun:kotlin.native.internal.test.TestRunner.run#internal + 2406
        14  test.kexe                           0x000000010e3da3e4 kfun:kotlin.native.internal.test.TestRunner.runIteration#internal + 2916
        15  test.kexe                           0x000000010e3dacee kfun:kotlin.native.internal.test.TestRunner.run()kotlin.Int + 958
        16  test.kexe                           0x000000010e3ce29d kfun:kotlin.native.internal.test.testLauncherEntryPoint(kotlin.Array<kotlin.String>)kotlin.Int + 301
        17  test.kexe                           0x000000010e3ce387 kfun:kotlin.native.internal.test.main(kotlin.Array<kotlin.String>) + 55
        18  test.kexe                           0x000000010e341315 Konan_start + 165
        19  test.kexe                           0x000000010e34ba1b Init_and_run_start + 107
        20  libdyld.dylib                       0x00007fff51a231fd start + 1

假设您需要不知道当天的平均滚动平均值。

答案 1 :(得分:0)

您可以只使用hour()函数:

select hour(timestamp) as hh, avg(sb1_ac_ges_diff)
from t
group by hh;

您可以根据需要将其转换为字符串或时间,但这对我而言似乎没有用。

如果您实际上想要每天的时间,那么:

select date(timestamp) as dd, hour(timestamp) as hh, avg(sb1_ac_ges_diff)
from t
group by dd, hh;