按日期时间字段分组数据

时间:2015-04-27 13:18:15

标签: mysql sql

我有以下表结构

| id                  | int(11)  | NO   | PRI | NULL    | auto_increment
| unique_id           | int(11)  | NO   | MUL | NULL    |
| date_time           | int(11)  | NO   | MUL | NULL    |
| float_value_1       | float    | NO   |     | 0       |
| float_value_2       | float    | YES  |     | NULL    |

此表中存储的数据表示间隔为5分钟的时间序列数据。

我试图通过将unique_id和date_time分组为15分钟间隔值,1小时间隔值和1天间隔值来计算MySQL查询来聚合浮点值。

我尝试了以下内容将给定unique_id的值聚合为1小时的间隔,但似乎我的聚合/分组概念不正确

select sum(float_value_1), sum(float_value_2), date_time*12 from location_data where unique_id = 1616 group by HOUR(date_time), day(date_time),  month(date_time) , year(date_time) ;

我想弄清楚如何为每个间隔(15分钟,1小时和1天)正确编写查询

提前致谢!

[编辑] 数据看起来像这样

   +----------+-------------+------------+---------------------+---------------------+---------------------+------------+
    | id       | unique_id | date_time  | float_value_1 | created_at          | updated_at          | float_value_2  |
    +----------+-------------+------------+---------------------+---------------------+---------------------+------------+
    | 27759971 |        1569 | 1410593400 |             37.7907 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |       NULL |
    | 27759972 |        1569 | 1410593700 |             38.9535 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    13.9535 |
    | 27759973 |        1569 | 1410594000 |             54.0698 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    181.395 |
    | 27759974 |        1569 | 1410594300 |             104.651 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    606.977 |
    | 27759975 |        1569 | 1410594600 |             98.2558 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -76.7442 |
    | 27759976 |        1569 | 1410594900 |             61.6279 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -439.535 |
    | 27759977 |        1569 | 1410595200 |             83.7209 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    265.116 |
    | 27759978 |        1569 | 1410595500 |             110.465 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |     320.93 |
    | 27759979 |        1569 | 1410595800 |             135.465 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |        300 |
    | 27759980 |        1569 | 1410596100 |             193.023 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    690.698 |
    | 27759981 |        1569 | 1410596400 |             206.977 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    167.442 |
    | 27759982 |        1569 | 1410596700 |             251.163 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    530.233 |
    | 27759983 |        1569 | 1410597000 |             338.372 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    1046.51 |
    | 27759984 |        1569 | 1410597300 |              504.07 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    1988.37 |
    | 27759985 |        1569 | 1410597600 |             503.488 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -6.97674 |
    | 27759986 |        1569 | 1410597900 |             333.721 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -2037.21 |
    | 27759987 |        1569 | 1410598200 |             284.884 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -586.047 |
    | 27759988 |        1569 | 1410598500 |             227.326 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -690.698 |
    | 27759989 |        1569 | 1410598800 |             218.605 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -104.651 |
    | 27759990 |        1569 | 1410599100 |             232.558 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    167.442 |
    | 27759991 |        1569 | 1410599400 |             293.605 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    732.558 |
    | 27759992 |        1569 | 1410599700 |             305.233 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    139.535 |
    | 27759993 |        1569 | 1410600000 |             303.488 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -20.9302 |
    | 27759994 |        1569 | 1410600300 |             258.721 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -537.209 |
    | 27759995 |        1569 | 1410600600 |             237.209 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    -258.14 |
    | 27759996 |        1569 | 1410600900 |             368.023 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    1569.77 |
    | 27759997 |        1569 | 1410601200 |             459.884 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    1102.33 |
    | 27759998 |        1569 | 1410601500 |             598.837 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    1667.44 |
    | 27759999 |        1569 | 1410601800 |              691.86 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    1116.28 |
    | 27760000 |        1569 | 1410602100 |             555.814 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -1632.56 |
    | 27760001 |        1569 | 1410602400 |             482.558 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    -879.07 |
    | 27760002 |        1569 | 1410602700 |             469.186 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -160.465 |
    | 27760003 |        1569 | 1410603000 |             450.581 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -223.256 |
    | 27760004 |        1569 | 1410603300 |             374.419 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -913.953 |
    | 27760005 |        1569 | 1410603600 |             338.372 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -432.558 |
    | 27760006 |        1569 | 1410603900 |             352.907 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    174.419 |
    | 27760007 |        1569 | 1410604200 |             359.884 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    83.7209 |
    | 27760008 |        1569 | 1410604500 |             398.256 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    460.465 |
    | 27760009 |        1569 | 1410604800 |             442.442 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    530.233 |
    | 27760010 |        1569 | 1410605100 |             418.605 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -286.047 |
    | 27760011 |        1569 | 1410605400 |             368.605 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |       -600 |
    | 27760012 |        1569 | 1410605700 |             370.349 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    20.9302 |
    | 27760013 |        1569 | 1410606000 |             468.605 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    1179.07 |
    | 27760014 |        1569 | 1410606300 |             628.488 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |     1918.6 |
    | 27760015 |        1569 | 1410606600 |             508.721 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -1437.21 |
    | 27760016 |        1569 | 1410606900 |             397.093 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -1339.53 |
    | 27760017 |        1569 | 1410607200 |             481.977 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |     1018.6 |
    | 27760018 |        1569 | 1410607500 |             451.163 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -369.767 |
    | 27760019 |        1569 | 1410607800 |             454.651 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    41.8605 |
    | 27760020 |        1569 | 1410608100 |             595.349 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    1688.37 |
    | 27760021 |        1569 | 1410608400 |             744.767 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    1793.02 |
    | 27760022 |        1569 | 1410608700 |             660.465 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -1011.63 |
    | 27760023 |        1569 | 1410609000 |             454.651 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -2469.77 |
    | 27760024 |        1569 | 1410609300 |             387.791 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -802.326 |
    | 27760025 |        1569 | 1410609600 |             349.419 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -460.465 |
    | 27760026 |        1569 | 1410609900 |             466.279 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    1402.33 |
    | 27760027 |        1569 | 1410610200 |             478.488 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    146.512 |
    | 27760028 |        1569 | 1410610500 |             412.209 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -795.349 |
    | 27760029 |        1569 | 1410610800 |             350.581 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -739.535 |
    | 27760030 |        1569 | 1410611100 |             322.674 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -334.884 |
    | 27760031 |        1569 | 1410611400 |             426.744 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    1248.84 |
    | 27760032 |        1569 | 1410611700 |             487.791 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    732.558 |
    | 27760033 |        1569 | 1410612000 |             472.093 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -188.372 |
    | 27760034 |        1569 | 1410612300 |             391.279 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -969.767 |
    | 27760035 |        1569 | 1410612600 |             352.326 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -467.442 |
    | 27760036 |        1569 | 1410612900 |             384.884 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    390.698 |
    | 27760037 |        1569 | 1410613200 |             397.674 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    153.488 |
    | 27760038 |        1569 | 1410613500 |              345.93 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    -620.93 |
    | 27760039 |        1569 | 1410613800 |             258.721 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -1046.51 |
    | 27760040 |        1569 | 1410614100 |             183.721 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |       -900 |
    | 27760041 |        1569 | 1410614400 |              170.93 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -153.488 |
    | 27760042 |        1569 | 1410614700 |             259.884 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    1067.44 |
    | 27760043 |        1569 | 1410615000 |             381.395 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    1458.14 |
    | 27760044 |        1569 | 1410615300 |              266.86 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -1374.42 |
    | 27760045 |        1569 | 1410615600 |             194.767 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -865.116 |
    | 27760046 |        1569 | 1410615900 |             261.047 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    795.349 |
    | 27760047 |        1569 | 1410616200 |              291.86 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    369.767 |
    | 27760048 |        1569 | 1410616500 |              304.07 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    146.512 |
    | 27760049 |        1569 | 1410616800 |             286.628 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -209.302 |
    | 27760050 |        1569 | 1410617100 |             279.651 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -83.7209 |
    | 27760051 |        1569 | 1410617400 |             283.721 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    48.8372 |
    | 27760052 |        1569 | 1410617700 |             414.535 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    1569.77 |
    | 27760053 |        1569 | 1410618000 |             613.953 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    2393.02 |
    | 27760054 |        1569 | 1410618300 |              545.93 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -816.279 |
    | 27760055 |        1569 | 1410618600 |             522.093 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -286.047 |
    | 27760056 |        1569 | 1410618900 |             417.442 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -1255.81 |
    | 27760057 |        1569 | 1410619200 |             323.837 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -1123.26 |
    | 27760058 |        1569 | 1410619500 |             401.744 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    934.884 |
    | 27760059 |        1569 | 1410619800 |             387.791 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -167.442 |
    | 27760060 |        1569 | 1410620100 |             376.163 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -139.535 |
    | 27760061 |        1569 | 1410620400 |             445.349 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    830.233 |
    | 27760062 |        1569 | 1410620700 |             431.395 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -167.442 |
    | 27760063 |        1569 | 1410621000 |             434.302 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    34.8837 |
    | 27760064 |        1569 | 1410621300 |             547.093 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    1353.49 |
    | 27760065 |        1569 | 1410621600 |             557.558 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    125.581 |
    | 27760066 |        1569 | 1410621900 |             484.302 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    -879.07 |
    | 27760067 |        1569 | 1410622200 |             482.558 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -20.9302 |
    | 27760068 |        1569 | 1410622500 |             675.581 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    2316.28 |
    | 27760069 |        1569 | 1410622800 |             807.558 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |    1583.72 |
    | 27760070 |        1569 | 1410623100 |             773.837 | 2015-04-23 11:57:39 | 2015-04-23 11:57:39 |   -404.651 | 
.. etc

按小时分组的输出数据示例

+----------------+--------------------+----------------------+
| date_time_n_hr | sum_float_value_1  | sum_flaot_value_2    |
+----------------+--------------------+----------------------+
|     1410591600 |  395.3488349914551 |    286.0465211868286 |
|     1410595200 | 3172.6744232177734 |    1988.371980190277 |
|     1410598800 |  4523.837203979492 |   3941.8605613708496 |
|     1410602400 |  4826.162811279297 |  -2225.5813941955566 |
|     1410606000 | 6233.7208251953125 |   209.30242156982422 |
|     1410609600 | 4894.7674560546875 |   -34.88380432128906 |
|     1410613200 | 3316.8604736328125 |   -969.7673645019531 |
|     1410616800 |  4853.488342285156 |    865.1161994934082 |
|     1410620400 |  6665.697814941406 |    544.1861915588379 |
|     1410624000 |  7529.069732666016 |     4102.32551574707 |
|     1410627600 |  3620.930221557617 |   -7981.395439147949 |
|     1410631200 |  979.6511611938477 |   -481.3953628540039 |
|     1410634800 | 205.23256015777588 |   -530.2325592041016 |
|     1410674400 |  8.720930099487305 | -0.45500504970550537 |
|     1410678000 | 1950.0000143051147 |     4723.25577545166 |
|     1410681600 |  9331.976806640625 |     8741.86051940918 |
|     1410685200 |   17227.9072265625 |    7179.069899082184 |
|     1410688800 | 23849.418334960938 |   2183.7211303710938 |
|     1410692400 | 26435.465087890625 |    2762.790672302246 |
|     1410696000 | 27538.953735351562 |    739.5348663330078 |
|     1410699600 |    25251.744140625 |   3920.9302978515625 |
|     1410703200 |  23297.67431640625 |   -7758.139587402344 |
|     1410706800 |  16787.20928955078 |    -10772.0927734375 |
|     1410710400 |  10172.67431640625 |   -3223.255828857422 |
.. etc

1 个答案:

答案 0 :(得分:1)

您按年,月,日和小时正确分组。但你展示的是不同的。什么是date_time * 12应该代表什么?首先,你随机抓取一个与小时匹配的date_times。然后你将它乘以12.假设date_time是2015年3月6日上午11:00。当你乘以那个日期时间时,你认为你得到了什么?这根本没有意义。

正确的查询将是例如:

select 
  date_format(date_time,'%Y%m%d %H'),
  sum(float_value_1), 
  sum(float_value_2)
from location_data 
where unique_id = 1616 
group by date_format(date_time,'%Y%m%d %H');

如果你想要一小时的时间,它会变得有点复杂,因为小时,分钟和秒是测量值,但是四分之一小时是这样一个量度(小时)的量(四分之一)。因此,我们必须找出date_time所在的四分之一小时:

select 
  concat(
    date_format(date_time,'%Y%m%d %H'),
    case
      when minute(date_time) between  0 and 14 then 'Q1'
      when minute(date_time) between 15 and 29 then 'Q2'
      when minute(date_time) between 30 and 44 then 'Q3'
      when minute(date_time) between 45 and 59 then 'Q4'
    end
  ) as quarter,
  sum(float_value_1), 
  sum(float_value_2)
from location_data 
where unique_id = 1616 
group by 
  concat(
    date_format(date_time,'%Y%m%d %H'),
    case
      when minute(date_time) between  0 and 14 then 'Q1'
      when minute(date_time) between 15 and 29 then 'Q2'
      when minute(date_time) between 30 and 44 then 'Q3'
      when minute(date_time) between 45 and 59 then 'Q4'
    end
  );

或者使用派生表,以避免重复评估季度:

select 
  quarter, sum(float_value_1), sum(float_value_2)
from
(
  select 
    location_data.*,
    concat(
      date_format(date_time,'%Y%m%d %H'),
      case
        when minute(date_time) between  0 and 14 then 'Q1'
        when minute(date_time) between 15 and 29 then 'Q2'
        when minute(date_time) between 30 and 44 then 'Q3'
        when minute(date_time) between 45 and 59 then 'Q4'
      end
    ) as quarter
  from location_data 
  where unique_id = 1616 
) as quarters
group by quarter;