1
1
123 1232014-11-19 23:54:07
MySQL
123 123, 2014-11-19 23:54:07

How to select data for each hour from SQL database?

Good day to all. I have a table with fields - (id, temp, hum, date). An entry is added to it every 5 minutes. Help write a query that would display temp, hum and date for each hour for the current day. That is, there should be 24 entries. I am attaching an image of the table for clarity. Thanks in advance.6fcd09d2f1064f63ac5cdbce72d60121.PNG

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
WhoMe, 2014-11-20
@mistergalynsky

Something like this

...
WHERE 
 DATE(date) = CURDATE()
GROUP BY
  EXTRACT(HOUR FROM date)
ORDER BY
 EXTRACT(HOUR FROM date)

But 24 entries will be no earlier than 12 am.
It needs to be clarified. What does "choose temp, hum and date" mean? Their sum, maximum/minimum or what? There are about 12 of them in an hour, and no more than 24 records are assumed in total.

M
Maxim Grechushnikov, 2014-11-20
@maxyc_webber

where date between 00000000 and 0000000

S
Sergey Eremin, 2020-06-15
@Sergei_Erjemin

If the data is not for one day and you need to get data for the entire period, then something like this:

SELECT
  DATE(table.dattim) AS DD,
  HOUR(table.dattim) AS HH,
  AVG(table.value) AS AVG_VALUE
FROM table
GROUP BY DD, HH
ORDER BY DD, HH

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question