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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:16:49+00:00 2026-05-22T02:16:49+00:00

So, I have got following data in a MySQL table: mysql> SELECT * FROM

  • 0

So, I have got following data in a MySQL table:

mysql> SELECT * FROM reading LIMIT 0,10;
+----+---------------------+-------+-------------+
| id | date                | power | temperature |
+----+---------------------+-------+-------------+
|  1 | 2011-02-15 21:58:41 |   124 |          22 |
|  2 | 2011-02-15 21:58:47 |   123 |          22 |
|  3 | 2011-02-15 21:58:53 |   124 |          22 |
|  4 | 2011-02-15 21:58:59 |   686 |          22 |
|  5 | 2011-02-15 21:59:05 |   126 |          22 |
|  6 | 2011-02-15 21:59:11 |   123 |          22 |
|  7 | 2011-02-15 21:59:17 |   122 |          22 |
|  8 | 2011-02-15 21:59:47 |   122 |          22 |
|  9 | 2011-02-15 21:59:59 |   122 |          22 |
| 10 | 2011-02-15 22:00:29 |   123 |          22 |
+----+---------------------+-------+-------------+
10 rows in set (0.00 sec)

(it is not a secret these are readings from Current Cost device)

Now, I have been battling SQL for few hours to get a average power amount over a 15 minutes period. The closest I could get is:

SELECT
    CONCAT( 
        DATE(date), ' ',
        HOUR(date), ':',
        CASE
            WHEN MINUTE(date) BETWEEN  0 AND 14 THEN '00'
            WHEN MINUTE(date) BETWEEN 15 AND 29 THEN '15'
            WHEN MINUTE(date) BETWEEN 30 AND 44 THEN '30'
            WHEN MINUTE(date) BETWEEN 45 AND 59 THEN '45'
        END
    ) AS date,
    ROUND(AVG(power),1)
FROM reading
WHERE date > '2011-02-20' AND date < '2011-02-21'
GROUP BY date;

Which gives me result as following:

mysql> SELECT
    ->     CONCAT( 
    ->         DATE(date), ' ',
    ->         HOUR(date), ':',
    ->         CASE
    ->             WHEN MINUTE(date) BETWEEN  0 AND 14 THEN '00'
    ->             WHEN MINUTE(date) BETWEEN 15 AND 29 THEN '15'
    ->             WHEN MINUTE(date) BETWEEN 30 AND 44 THEN '30'
    ->             WHEN MINUTE(date) BETWEEN 45 AND 59 THEN '45'
    ->         END
    ->     ) AS date,
    -> ROUND(AVG(power),1)
    -> FROM reading
    -> WHERE date > '2011-02-20' AND date < '2011-02-21'
    -> GROUP BY date LIMIT 0,10;
+-----------------+---------------------+
| date            | ROUND(AVG(power),1) |
+-----------------+---------------------+
| 2011-02-20 0:00 |               320.0 |
| 2011-02-20 0:00 |               319.0 |
| 2011-02-20 0:00 |               317.0 |
| 2011-02-20 0:00 |               313.0 |
| 2011-02-20 0:00 |               315.0 |
| 2011-02-20 0:00 |               315.0 |
| 2011-02-20 0:00 |               320.0 |
| 2011-02-20 0:00 |               315.0 |
| 2011-02-20 0:00 |               315.0 |
| 2011-02-20 0:00 |               313.0 |
[...]
| 2011-02-20 0:45 |               316.0 |
| 2011-02-20 0:45 |               311.0 |
| 2011-02-20 0:45 |               310.0 |
| 2011-02-20 0:45 |               317.0 |
| 2011-02-20 0:45 |               311.0 |
| 2011-02-20 0:45 |               312.0 |
| 2011-02-20 0:45 |               318.0 |
| 2011-02-20 0:45 |               315.0 |
| 2011-02-20 0:45 |               313.0 |
| 2011-02-20 0:45 |               316.0 |
| 2011-02-20 0:45 |               311.0 |
| 2011-02-20 0:45 |               312.0 |
| 2011-02-20 0:45 |               319.0 |
| 2011-02-20 0:45 |               312.0 |
[...]
| 2011-02-20 4:00 |                95.0 |
| 2011-02-20 4:15 |                95.0 |
| 2011-02-20 4:15 |                95.0 |
| 2011-02-20 4:15 |                94.0 |
| 2011-02-20 4:15 |                94.0 |
| 2011-02-20 4:15 |                94.0 |
| 2011-02-20 4:15 |                94.0 |
| 2011-02-20 4:15 |                94.0 |
| 2011-02-20 4:15 |                94.0 |
| 2011-02-20 4:15 |                96.0 |
| 2011-02-20 4:15 |                93.0 |
| 2011-02-20 4:15 |                94.0 |
| 2011-02-20 4:15 |                93.0 |
| 2011-02-20 4:15 |                93.0 |
+-----------------+---------------------+
10 rows in set, 1 warning (0.20 sec)

Which is still quite far away from what I am trying to get. It seems like I am very close to what I need, just something to DISTINCT the date field and get averages from all equal dates fields, but I just can’t get the correct SQL.

The result I am aiming for is something along lines:

| 2011-02-20 4:00 |                94.0 |
| 2011-02-20 4:15 |               194.3 |
| 2011-02-20 4:30 |               197.4 |
| 2011-02-20 4:45 |               145.3 |
| 2011-02-20 5:00 |                94.0 |
| 2011-02-20 5:15 |                96.0 |
| 2011-02-20 5:30 |                93.0 |
| 2011-02-20 5:45 |                94.0 |

Any help or hints much appreciated.

  • 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-22T02:16:49+00:00Added an answer on May 22, 2026 at 2:16 am

    MySQL is getting confused because you’ve aliased your CONCAT() output to the same name as an existing column, so GROUP BY date is grouping by the table’s date field, not your rounded-to-the-nearest-15-minutes date.

    You’ll need to change your query to something like

    mysql> SELECT
        ->     CONCAT( 
        ->         DATE(date), ' ',
        ->         HOUR(date), ':',
        ->         CASE
        ->             WHEN MINUTE(date) BETWEEN  0 AND 14 THEN '00'
        ->             WHEN MINUTE(date) BETWEEN 15 AND 29 THEN '15'
        ->             WHEN MINUTE(date) BETWEEN 30 AND 44 THEN '30'
        ->             WHEN MINUTE(date) BETWEEN 45 AND 59 THEN '45'
        ->         END
        ->     ) AS quarterhour,
        -> ROUND(AVG(power),1)
        -> FROM reading
        -> WHERE date > '2011-02-20' AND date < '2011-02-21'
        -> GROUP BY quarterhour LIMIT 0,10;
    

    Also, instead of HOUR() I would use DATE_FORMAT() in order to zeropad the hours, otherwise “5” (5AM) comes after “17” (5PM).

    CONCAT(DATE_FORMAT(date,'%Y-%m-%d %H:'), CASE ... END) should do the job.

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

Sidebar

Related Questions

I have got the following code from here to read an Excel file using
I have got the following problem since the server has safe mode turned on,
I've got code similar to the following... <p><label>Do you have buffet facilities?</label> <asp:RadioButtonList ID=blnBuffetMealFacilities:chk
Got a bit of a mind freeze at the moment. I have the following
We have got loads of options for php + MySQL + Apache combo... Which
I have got a table in MS Access 2007 with 4 fields. Labour Cost
I've got the following problem: I have two tables: (simplified) +--------+ +-----------+ | User
I have the following data Animals = Dog Cat Turtle \ Mouse Parrot \
I've got following java class. package com.org.data.dbresource; import org.springframework.orm.ibatis.SqlMapClientTemplate; public class DBConnectionManager { private
I got the following situation: I have an application configuration in my database which

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.