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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:44:48+00:00 2026-05-29T09:44:48+00:00

I’ve got a database consisting of the following Columns: time | windSpeed | temperature

  • 0

I’ve got a database consisting of the following Columns:

time | windSpeed | temperature | more parameters

Every 30 seconds a row is automatically added with the current wind speed, temperature etc.

To be able to display a graph I would like to construct a MySQL Query which displays the average per hour for 24 hours starting from the newest entry.
So the new relation/table should look like this:

hour | avgWindspeed | avgTemperature
-1   | 13.3         | -5.8
-2   | 16.4         | -3.1

This is how far I got:
Each row should me made up of 60minutes / 30 seconds = 120 rows.

SELECT * 
         FROM (
                   SELECT @row := @row +1 AS rownum, tijd, wind_s, luchtdruk, temp
                   FROM (
                         SELECT @row :=0
                         )r, weer_actueel
                   ORDER BY tijd DESC
         )ranked
         WHERE rownum %120 =1

It now displays the 120th row instead of displaying the average of the 120 rows.

Could anyone help me?

Edit 1:

  • the programming language I used is PHP; but I would love a native MySQL solution.
  • the column ‘time’ is a mysql datetime value
  • 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-29T09:44:49+00:00Added an answer on May 29, 2026 at 9:44 am

    By grouping by “Per Hour”, you get all hours broken down. The WHERE clause always goes explicitly 24 hours from whatever is the current date/time, so this will even account for a time of 14:37 in the afternoon to one day prior 14:37.
    Then, by using the min( w.time ), we are getting the first actual full date/time stamp for the order by so we can sort it by most recent time at the top going backwards as what would be the correct order if the time is 7 in the morning and you are going backwards to hour 23 of the prior day, down to hour 8. This makes sure its by proper time.

    select
          hour( w.time ) as PerHour,
          avg( w.WindSpeed ) as AvgWind,
          avg( w.Temperature ) as AvgTemp,
          min( w.time ) as StartingTimeForGroup
       from
          weer_actueel w
       where
          w.time >= date_sub( curdate(), interval 24 hour )
       group by
          PerHour
       order by
          StartingTimeForGroup  DESC
    

    If you want the breaking to exactly be based on the minute within the hour break… such as

    13:48 - 12:49
    12:48 - 11:49
    11:48 - 10:48
    

    that will take a bit more finesse by pre-querying to detect that starting point which will result in a large case construct of 24 hours, but we can even build that out with @sql variables… Try this..

    select
          HourlyGaps.HoursAgo,
          avg( w.WindSpeed ) as AvgWind,
          avg( w.Temperature ) as AvgTemp
       from
          ( select
                  @MyHour := @MyHour +1 as HoursAgo,
                  @CurTime as EndTime,
                  @CurTime := date_sub( @CurTime, interval 1 hour ) as StartTime
               from
                  ( select @MyHour := 0,
                           @CurTime := now()) SqlVars,
                  weer_actueeel w2
               limit 24 ) HourlyGaps
           JOIN weer_actueel w
              on w.time > HourlyGaps.StartTime 
              and w.time <= HourlyGaps.EndTime
       group by
          HoursAgo
       order by
          HoursAgo
    

    The inner query with the sql vars primes the MyHour with 0 and CurTime with now() (date and time). Then does a Cartesian (no join) to the weer_actueel table with a limit of 24 records. When building out the results of this HourlyGaps inner query, the @Vars keep going backwards one hour at a time… The @CurTime starts as the end, then subtracts one hour from itself to become the beginning hour for that time slot… which then becomes the ending time for the next prior hour…. completes for a cycle of 24 hours.

    This is then re-joined to the stats table only for those times within the given matched time slots of 24 hours. Since we have the “@MyHour” being saved as HoursAgo, and that keeps increasing for each row, we also have that for the basis of proper sorted output.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I have a reasonable size flat file database of text documents mostly saved in
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In my XML file chapters tag has more chapter tag.i need to display chapters
I have just tried to save a simple *.rtf file with some websites and

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.