Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6238213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:10:21+00:00 2026-05-24T11:10:21+00:00

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

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:10:22+00:00Added an answer on May 24, 2026 at 11:10 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 5 million row table that gets hit with a specific query
I have a data structure that looks like this: Model Place primary key id
I have been building index on a 200 million row table for almost 14
I have a query that looks like this: select id , int1 , int2
I have a deep hierarchical model that looks like this: Accounts -> Venues ->Events
I have a million rows in a database table. For each row I have
I have a table with 42 million records. 32 million have a null value
I'm having some serious performance issues with a multi-million row table that I feel
Scenario I have a 10 million row table. I partition it into 10 partitions,
I have a InnoDB table that has about 17 normalized columns with ~6 million

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.