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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:38:38+00:00 2026-06-15T07:38:38+00:00

I have stored temperatures in a MySQL database. The table is called temperatures .

  • 0

I have stored temperatures in a MySQL database. The table is called temperatures. It contains, for example, the columns dtime and temperature. The first one is the time the temperature was measured (the column type is DATETIME) and the latter, well, apparently the temperature (the type is FLOAT).

At the moment I use the following query to fetch the temperatures in a certain period.

SELECT dtime, temperature 
FROM temperatures 
WHERE dtime BETWEEN "2012-11-15 00:00:00" AND "2012-11-30 23:59:59" 
ORDER BY dtime DESC

I’d like to add the average temperature of the day in the results. I tried the following.

SELECT 
    dtime AS cPVM,
    temperature,
    (
        SELECT AVG(temperature) 
        FROM temperatures 
        WHERE DATE(dtime) = DATE(cPVM)
    ) AS avg
FROM temperatures 
WHERE dtime BETWEEN "2012-11-15 00:00:00" AND "2012-11-30 23:59:59" 
ORDER BY dtime DESC

Works ok, but this is really, really slow. Fetching the results in that period takes about 5 seconds, when the first one (without the averages) is done in 0.03 seconds.

SELECT DATE(dtime), AVG(temperature) 
FROM temperatures 
WHERE DATE(dtime) BETWEEN "2012-11-15" AND "2012-11-30" 
GROUP BY DATE(dtime) 
ORDER BY dtime DESC

This one however is done in 0.04 seconds.

How do I fetch the average temperatures more efficiently?

  • 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-15T07:38:40+00:00Added an answer on June 15, 2026 at 7:38 am

    Use a join instead of a correlated subquery:

    SELECT dtime, temperature, avg_temperature
    FROM temperatures 
    JOIN (
        SELECT DATE(dtime) AS date_dtime, AVG(temperature) AS avg_temperature
        FROM temperatures
        WHERE dtime >= '2012-11-15' AND dtime < '2012-12-01'
        GROUP BY DATE(dtime)
    ) AS avg_t
    ON date_dtime = DATE(dtime)
    WHERE dtime dtime >= '2012-11-15' AND dtime < '2012-12-01'
    ORDER BY dtime DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a temperature measurements stored in a table, each measure have actuall date/time
I have stored various records in the MySql database orkut. Now I want to
I have stored procedure that insanely times out every single time it's called from
I have stored some data in MySQL database through a java program. Now I
I have created an SQLite database which has a table which stores temperature values.
I have stored procedure in my database and i need to look up a
I have stored a variable in database(sqlite) with datetime data type. Now I want
I have stored 6 records in mysql db,when i use this code each of
i have stored the txt file to sql server database . i need to
I have stored some names and scores in a SQLite Database, as shown in

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.