druzuz321103 2011-08-09 14:29 采纳率: 100%
浏览 53
已采纳

MySQL平均每4行

I have a 10 million+ row table that looks like this:

    id | time_read  | value
    -----------------------
    9   1111111111   100
    9   1111111222   200
    9   1111111333   150
    9   1111111444   225

I want to AVG the value of every 4 rows. How do I do this in MySQL?

Also, secondary to my question, how might I do this if I wanted to avg the value of every 4 rows for all the data in a month and return that total for each month of each year in the table? For example, using the same table in my first question, ending up with:

    2008 | 12 | 500000
    2009 | 01 | 450000
    2009 | 02 | 475000

edit: In other words, the total of each average 4 rows grouped by year-month. Each time_read is 15 minutes apart.

I was doing something like this previously, but it wasn't accurate enough. I need to average every 4 rows and total that instead of taking the sum of all value's in a month and dividing by 4.

    SELECT DATE_FORMAT(FROM_UNIXTIME(time_read),'%Y %m') as tr,  
    DATE_FORMAT(FROM_UNIXTIME(time_read),'%Y') as year, 
    DATE_FORMAT(FROM_UNIXTIME(time_read),'%m') as month, SUM(value) as value 
    FROM table WHERE id = 9 
    GROUP BY tr
  • 写回答

3条回答 默认 最新

  • doushi7819 2011-08-09 15:07
    关注

    Try this code -

    CREATE TABLE table1 (
      id INT(11) NOT NULL AUTO_INCREMENT,
      time_read INT(11) DEFAULT NULL,
      value INT(11) DEFAULT NULL,
      PRIMARY KEY (id)
    );
    
    INSERT INTO table1 VALUES 
      (1, 1312880400, 10),  -- 09.08.2011 12:00:00 -> 1 august
      (2, 1312880410, 20),  -- 09.08.2011 12:00:10 -> 2
      (3, 1312880420, 30),  -- 09.08.2011 12:00:20 -> 3
      (4, 1312880430, 40),  -- 09.08.2011 12:00:30 -> 4
      (5, 1312880440, 50),  -- 09.08.2011 12:00:40 -> 5
      (6, 1315558800, 60),  -- 09.09.2011 12:00:00 -> 1 september
      (7, 1315558810, 70);  -- 09.09.2011 12:00:10 -> 2
    
    SELECT AVG(value) FROM (
      SELECT t1.*, COUNT(*) cnt FROM table1 t1
      LEFT JOIN table1 t2
        ON t2.time_read <= t1.time_read
          AND YEAR(FROM_UNIXTIME(t2.time_read)) = YEAR(FROM_UNIXTIME(t1.time_read))
          AND MONTH(FROM_UNIXTIME(t2.time_read)) = MONTH(FROM_UNIXTIME(t1.time_read))
      GROUP 
        BY time_read
    ) t
    GROUP BY
      YEAR(FROM_UNIXTIME(time_read)), MONTH(FROM_UNIXTIME(time_read)), CEIL(cnt/4);
    
    +------------+
    | AVG(value) |
    +------------+
    |    25.0000 |
    |    50.0000 |
    |    65.0000 |
    +------------+
    

    It does group by month and 4 records in month.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题