1
1
123 1232014-10-26 05:36:57
PHP
123 123, 2014-10-26 05:36:57

How to select data for each month for a year?

Good day to all. In general, the task is the following. You need to select data (more precisely, the average maximum and minimum temperatures) for each month for the current year. To draw a graph for me to have months, I start from January to December and each month has 3 columns (avg.temp., max.temp. and min.temp.). Help write SQL query. I am attaching an image below to make it clear what I need. Thanks in advance.
c74acc7b7f674fd6b0101196983fa40e.PNG

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Romanov, 2014-10-26
@mistergalynsky

There is not a lot of initial data, so something like that. Basic principle only.

SELECT 
   MAX(temp) AS Maximum
   MIN(temp) AS Minimum
   AVG(temp) AS Average
  DATE_FORMAT(ctime, '%c') AS Month
FROM temperature
WHERE ctime > NOW() - INTERVAL 1 YEAR AND ctime < NOW()
GROUP BY  Month
ORDER BY ctime DESC

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question