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 8504699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:06:32+00:00 2026-06-11T02:06:32+00:00

I have a problem with MySQL. I need the average of data from MySQL

  • 0

I have a problem with MySQL. I need the average of data from MySQL between two particular times.

For that, I’m using:

select avg(Column) 
from Databasename.tablename 
where datetime BETWEEN '2012-09-08 00:00:00' AND '2012-09-08 15:30:00'

Now, what I need is the average of data for every 5 minutes, in the given times. By using this:

select avg(Column) 
from Databasename.Tablename 
where datetime BETWEEN '2012-09-08 15:00:00' 
                   AND '2012-09-08 15:30:00' + 'INTERVAL 5minutes'

It’s not showing any error but showing NULL.

How do I get the data for every 5mins within two particular time periods?

  • 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-06-11T02:06:33+00:00Added an answer on June 11, 2026 at 2:06 am

    Group to intervals

    You should use group by to create one group for each interval over which you want to average.

    select avg(Column), convert((min(datetime) div 500)*500 + 230, datetime) as time
    from Databasename.tablename 
    where datetime BETWEEN '2012-09-08 00:00:00' AND '2012-09-08 15:30:00'
    group by datetime div 500
    

    This makes use of the fact that a date and time can implicitely be converted to a number, which will then be of the format YYMMDDhhmmss. Dividing such a number by 100 strips the seconds, and similarly dividing by 500 yields a unique number for every 5-minute interval.

    The additional column I selected will give the middle of the interval: any time from the interval (here choosing the minimum, but it doesn’t really matter), turned into that 5-minute interval number, back into a date-and-time number, and then adding two and a half minutes. You might use similar syntax to compute the beginning (simply leave the + 230 out) or (inclusive) end (+ 459) of the interval.

    Tested using sqlfiddle.

    End of range handling

    Note that your BETWEEN range will include rows from 15:30:00 but no others from the interval started at that time. Maybe you want to exclude the end of the range from your selection:

    where datetime >= '2012-09-08 00:00:00' AND datetime < '2012-09-08 15:30:00'
    

    Why you get NULL

    As to why your query yields NULL: To do interval arithmetic, you don’t enclose the whole interval thing in quotes, and you use singular names for time units. I.e.

    '2012-09-08 15:30:00' + INTERVAL 5 minute
    

    But this would simply extend the range to 2012-09-08 15:35:00 and still not create those 5-minute intervals you want. The way you wrote it, you’re trying to add two strings, which isn’t possible in MySQL unless the strings can be converted to numbers, in which case those numbers will be added.

    You might want to use interval arithmetic to compute the exclusive end of the interval, i.e. the first second after the interval:

     convert((min(datetime) div 500)*500, datetime) + INTERVAL 5 minute as endOfInterval
    

    Simply adding 500 instead of the 230 of my query won’t work as the resulting number might represent an invalid date for the last interval of each hour.

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

Sidebar

Related Questions

I have a mysql problem. I have two tables like this that I need
I have a problem with displaying data from mysql database. My client requires to
I have a problem while I need to send data using uploadify script. For
I have a problem in mysql. I need to fetch certain sets of records
I have some problem with MySQL Workbench in that I sometimes can't set foreign
I have a problem with MySql.Data in a partial trusted environment. I've added MySql.Data
I have a mysql problem, my query looks as follows but not complete SELECT
I have problem with creating a simple MySQL trigger in C#. I'm using StringBuilder
I am using CakePHP framework with MySQL database and I have problem in saving
i have about 140 - 150 stored procedures that I need to migrate from

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.