How to select the oldest date from different columns? - mysql

I know I could use min(date) to find the oldest date between a columns but how can I select the oldest date between 3 different columns?
Example:
Select date_1, date_2, date_3, do_something(oldest_date) from table;
EDIT: Actually my query is a little bit more complicated.
SELECT
(SELECT
IF(test.OLD = 1, DATE_FORMAT(test.DATE_EXPIRATION, '%d-%m-%Y'), IF(revalidation.recognized = 0, DATE_FORMAT(DATE_ADD(revalidation.DATE_test, INTERVAL revalidation.VALIDITY YEAR), '%d-%m-%Y'), IF(test.DATE_EXPIRATION = ''
OR test.DATE_EXPIRATION IS NULL, DATE_FORMAT(DATE_ADD(test.DATE_test, INTERVAL test.VALIDITY YEAR), '%d-%m-%Y'), DATE_FORMAT(test.DATE_EXPIRATION, '%d-%m-%Y')))) AS VENCIMIENTO
FROM
eva_compet revalidation
LEFT JOIN eva_compet test ON (revalidation.recognized = test.EVA_LAB_ID)
WHERE
revalidation.TYPO_EVA_ID = 1
AND revalidation.LABORAL_ID = cl.laboral_id
LIMIT 1) AS DATE_1,
(SELECT
IF(test.OLD = 1, DATE_FORMAT(test.DATE_EXPIRATION, '%d-%m-%Y'), IF(revalidation.recognized = 0, DATE_FORMAT(DATE_ADD(revalidation.DATE_test, INTERVAL revalidation.VALIDITY YEAR), '%d-%m-%Y'), IF(test.DATE_EXPIRATION = ''
OR test.DATE_EXPIRATION IS NULL, DATE_FORMAT(DATE_ADD(test.DATE_test, INTERVAL test.VALIDITY YEAR), '%d-%m-%Y'), DATE_FORMAT(test.DATE_EXPIRATION, '%d-%m-%Y')))) AS VENCIMIENTO
FROM
eva_compet revalidation
LEFT JOIN eva_compet test ON (revalidation.recognized = test.EVA_LAB_ID)
WHERE
revalidation.TYPO_EVA_ID = 2
AND revalidation.LABORAL_ID = cl.laboral_id
LIMIT 1) AS DATE_2,
(SELECT
IF(test.OLD = 1, DATE_FORMAT(test.DATE_EXPIRATION, '%d-%m-%Y'), IF(revalidation.recognized = 0, DATE_FORMAT(DATE_ADD(revalidation.DATE_test, INTERVAL revalidation.VALIDITY YEAR), '%d-%m-%Y'), IF(test.DATE_EXPIRATION = ''
OR test.DATE_EXPIRATION IS NULL, DATE_FORMAT(DATE_ADD(test.DATE_test, INTERVAL test.VALIDITY YEAR), '%d-%m-%Y'), DATE_FORMAT(test.DATE_EXPIRATION, '%d-%m-%Y')))) AS VENCIMIENTO
FROM
eva_compet revalidation
LEFT JOIN eva_compet test ON (revalidation.recognized = test.EVA_LAB_ID)
WHERE
revalidation.TYPO_EVA_ID = 3
AND revalidation.LABORAL_ID = cl.laboral_id
LIMIT 1) AS DATE_3,
CASE WHEN DATE_1 <= DATE_2 AND DATE_1 <= DATE_3 THEN DATE_1
WHEN DATE_2 <= DATE_1 AND DATE_2 <= DATE_3 THEN DATE_2
ELSE DATE_3
END AS TERMINO
FROM
table
WHERE
table_id = 1001;
I tried to use the CASE as suggested but I am getting:
Error Code: 1054. Unknown column 'date_1' in 'field list'
SOLUTION (I followed the solution provided by #joe-stefanelli and the suggestion from #Uueerdo, THANKS ):
SELECT
DATE_1,
DATE_2,
DATE_3,
CASE WHEN DATE_1 <= DATE_2 AND DATE_1 <= DATE_3 THEN DATE_1
WHEN DATE_2 <= DATE_1 AND DATE_2 <= DATE_3 THEN DATE_2
ELSE DATE_3
END AS OLDEST_DATE
FROM
(SELECT
(SELECT
IF(test.OLD = 1, DATE_FORMAT(test.DATE_EXPIRATION, '%d-%m-%Y'), IF(revalidation.recognized = 0, DATE_FORMAT(DATE_ADD(revalidation.DATE_test, INTERVAL revalidation.VALIDITY YEAR), '%d-%m-%Y'), IF(test.DATE_EXPIRATION = ''
OR test.DATE_EXPIRATION IS NULL, DATE_FORMAT(DATE_ADD(test.DATE_test, INTERVAL test.VALIDITY YEAR), '%d-%m-%Y'), DATE_FORMAT(test.DATE_EXPIRATION, '%d-%m-%Y')))) AS VENCIMIENTO
FROM
eva_compet revalidation
LEFT JOIN eva_compet test ON (revalidation.recognized = test.EVA_LAB_ID)
WHERE
revalidation.TYPO_EVA_ID = 1
AND revalidation.LABORAL_ID = cl.laboral_id
LIMIT 1) AS DATE_1,
(SELECT
IF(test.OLD = 1, DATE_FORMAT(test.DATE_EXPIRATION, '%d-%m-%Y'), IF(revalidation.recognized = 0, DATE_FORMAT(DATE_ADD(revalidation.DATE_test, INTERVAL revalidation.VALIDITY YEAR), '%d-%m-%Y'), IF(test.DATE_EXPIRATION = ''
OR test.DATE_EXPIRATION IS NULL, DATE_FORMAT(DATE_ADD(test.DATE_test, INTERVAL test.VALIDITY YEAR), '%d-%m-%Y'), DATE_FORMAT(test.DATE_EXPIRATION, '%d-%m-%Y')))) AS VENCIMIENTO
FROM
eva_compet revalidation
LEFT JOIN eva_compet test ON (revalidation.recognized = test.EVA_LAB_ID)
WHERE
revalidation.TYPO_EVA_ID = 2
AND revalidation.LABORAL_ID = cl.laboral_id
LIMIT 1) AS DATE_2,
(SELECT
IF(test.OLD = 1, DATE_FORMAT(test.DATE_EXPIRATION, '%d-%m-%Y'), IF(revalidation.recognized = 0, DATE_FORMAT(DATE_ADD(revalidation.DATE_test, INTERVAL revalidation.VALIDITY YEAR), '%d-%m-%Y'), IF(test.DATE_EXPIRATION = ''
OR test.DATE_EXPIRATION IS NULL, DATE_FORMAT(DATE_ADD(test.DATE_test, INTERVAL test.VALIDITY YEAR), '%d-%m-%Y'), DATE_FORMAT(test.DATE_EXPIRATION, '%d-%m-%Y')))) AS VENCIMIENTO
FROM
eva_compet revalidation
LEFT JOIN eva_compet test ON (revalidation.recognized = test.EVA_LAB_ID)
WHERE
revalidation.TYPO_EVA_ID = 3
AND revalidation.LABORAL_ID = cl.laboral_id
LIMIT 1) AS DATE_3,
CASE WHEN DATE_1 <= DATE_2 AND DATE_1 <= DATE_3 THEN DATE_1
WHEN DATE_2 <= DATE_1 AND DATE_2 <= DATE_3 THEN DATE_2
ELSE DATE_3
END AS TERMINO
FROM
table
WHERE
table_id = 1001)
AS table_2;

SELECT CASE WHEN date_1 <= date_2 AND date_1 <= date_3 THEN date_1
WHEN date_2 <= date_1 AND date_2 <= date_3 THEN date_2
ELSE date_3
END AS oldest_date
FROM table;

This answers the original post:
select date_1, date_2, date_3, do_something(a) from table, (select least(date_1, date_2, date_3) a from table) b;
I'm not going to try to figure out the complexities of the edit.
If all you want is the results of your function, then this:
select do_something(a) from (select least(date_1, date_2, date_3) a from table) b;

Okay, I took a stab at it with the complexities. I'm not sure if this will work:
SELECT DATE_1, DATE_2, DATE_3, do_something(a) FROM b,
( SELECT LEAST(DATE_1, DATE_2, DATE_3) a FROM
( SELECT (...) as DATE_1, (...) as DATE_2, (...) as DATE_3 )
FROM table WHERE table_id = 1001
) AS b
) AS c;

Related

Can you use math calculations on two alias columns in MySQL?

I want to do this:
SELECT SUM(currentbalance) AS 'current', SUM(principal) AS 'prin', 'current' + 'prin' AS 'total'
FROM dbase
This doesn't work. I've also tried:
SELECT sum(currentbalance) AS 'current', sum(principal) AS 'prin', (SELECT 'current') + (SELECT 'prin') AS 'total'
FROM dbase
#juergen-d #fqhv
Both answers worked perfectly. I thought that would be enough for me to understand how to add it to my current query. How can I implement this inside this query? I need to divide the "file_principal" by the "total_cash_cleared"? Thanks in Advance.
SELECT a.portfolio
,a.dateplaced
,(SELECT SUM(chargedoffprincipal) FROM Dbase WHERE portfolio = a.portfolio ) AS 'file_principal'
,SUM(IF(b.paymentstatus IN ('POSTED','PROCCESED'), paymentamount, 0)) AS 'total_cash_cleared'
,SUM(IF(b.paymentstatus = 'PENDING', paymentamount, 0)) AS 'total_cash_pending'
,SUM(IF(b.paymentstatus = 'PENDING' AND YEAR(paymentdate) = YEAR(CURRENT_DATE + INTERVAL 1 MONTH) AND MONTH(paymentdate) = MONTH(CURRENT_DATE + INTERVAL 1 MONTH),paymentamount, 0)) AS '30_days'
,SUM(IF(b.paymentstatus = 'PENDING' AND YEAR(paymentdate) = YEAR(CURRENT_DATE + INTERVAL 1 MONTH) AND MONTH(paymentdate) = MONTH(CURRENT_DATE + INTERVAL 2 MONTH),paymentamount, 0)) AS '60_days'
,SUM(IF(b.paymentstatus = 'PENDING' AND YEAR(paymentdate) = YEAR(CURRENT_DATE + INTERVAL 1 MONTH) AND MONTH(paymentdate) = MONTH(CURRENT_DATE + INTERVAL 3 MONTH),paymentamount, 0)) AS '90_days'
FROM dbase a
LEFT JOIN payments b
ON a.filenumber = b.filenumber
GROUP BY a.portfolio
ORDER BY STR_TO_DATE(a.dateplaced, '%m/%d/%Y') DESC
I tried to add this in a comment but it was too long so I edited this post sorry.
You can't reuse aliases in the same SELECT clause
SELECT SUM(currentbalance) AS 'current',
SUM(principal) AS 'prin',
SUM(currentbalance + principal) AS 'total'
FROM dbase
I like juergen's answer better, but you could add using aliases if you put it in a subquery.
SELECT [current] + [prin]
FROM (
SELECT
SUM(currentbalance) AS 'current',
SUM(principal) AS 'prin'
FROM dbase ) x
Thanks to my slack buddy here is the way to handle this. Makes all the sense now.
SELECT
portfolio,
dateplaced,
file_principal,
total_cash_cleared,
IF(total_cash_cleared <> 0, file_principal / total_cash_cleared, 0) AS div_result,
total_cash_pending,
30_days,
60_days,
90_days
FROM
(SELECT a.portfolio
,a.dateplaced
,(SELECT SUM(chargedoffprincipal) FROM Dbase WHERE portfolio = a.portfolio ) AS 'file_principal'
,SUM(IF(b.paymentstatus IN ('POSTED','PROCCESED'), paymentamount, 0)) AS 'total_cash_cleared'
,SUM(IF(b.paymentstatus = 'PENDING', paymentamount, 0)) AS 'total_cash_pending'
,SUM(IF(b.paymentstatus = 'PENDING' AND YEAR(paymentdate) = YEAR(CURRENT_DATE + INTERVAL 1 MONTH) AND MONTH(paymentdate) = MONTH(CURRENT_DATE + INTERVAL 1 MONTH),paymentamount, 0)) AS '30_days'
,SUM(IF(b.paymentstatus = 'PENDING' AND YEAR(paymentdate) = YEAR(CURRENT_DATE + INTERVAL 1 MONTH) AND MONTH(paymentdate) = MONTH(CURRENT_DATE + INTERVAL 2 MONTH),paymentamount, 0)) AS '60_days'
,SUM(IF(b.paymentstatus = 'PENDING' AND YEAR(paymentdate) = YEAR(CURRENT_DATE + INTERVAL 1 MONTH) AND MONTH(paymentdate) = MONTH(CURRENT_DATE + INTERVAL 3 MONTH),paymentamount, 0)) AS '90_days'
FROM dbase a
LEFT JOIN payments b
ON a.filenumber = b.filenumber
GROUP BY a.portfolio
) AS original_query
ORDER BY STR_TO_DATE(original_query.dateplaced, '%m/%d/%Y') DESC

How to select data from 1 to 5 days with Group By

I've tried it here but I could not, I can only display the total.
I have a download table and a program table.
Every time I download a program I record the date and time, I need to do a grouping of downloaded programs and then 5 columns with the dates, here's an example.
PROGRAMA | HOJE | ONTEM| 2 DIAS | 3 DIAS | 4 DIAS
Programa 1 11 110 55 66 12
Programa 2 25 140 60 90 12
Programa 3 10 20 20 10 10
TOTAL 46 270 135 166 32
Below is my query
select `k`.`app_id` AS `app_id`,`b`.`aplicativo` AS `aplicativo`,count(0) AS `HOJE`,
(select count(0) AS `count(*)` from (`registration` `a` join `aplicativos` `b`) where `k`.`app_id`= `b`.`id` and created_at > (cast(now() as date) - interval 1 day) and (`a`.`created_at` < cast(now() as date)- interval 0 day) ) as ONTEM ,
(select count(0) AS `count(*)` from (`registration` `a` join `aplicativos` `b`) where `k`.`app_id` = `b`.`id`
and created_at > (cast(now() as date) - interval 2 day) and (`a`.`created_at` < cast(now() as date)- interval 1 day) ) as 2_DIAS_ANTES ,
(select count(0) AS `count(*)` from (`registration` `a` join `aplicativos` `b`) where `k`.`app_id` = `b`.`id`
and created_at > (cast(now() as date) - interval 3 day) and (`a`.`created_at` < cast(now() as date)- interval 2 day) ) as 3_DIAS_ANTES ,
(select count(0) AS `count(*)` from (`registration` `a` join `aplicativos` `b`) where `k`.`app_id` = `b`.`id`
and created_at > (cast(now() as date) - interval 4 day) and (`a`.`created_at` < cast(now() as date)- interval 3 day) ) as 4_DIAS_ANTES ,
(select count(0) AS `count(*)` from (`registration` `a` join `aplicativos` `b`) where `k`.`app_id` = `b`.`id`
and created_at > (cast(now() as date) - interval 5 day) and (`a`.`created_at` < cast(now() as date)- interval 4 day) ) as 5_DIAS_ANTES
from (`registration` `k` join `aplicativos` `b`) where ((`k`.`app_id` = `b`.`id`) and (`k`.`created_at` > (cast(now() as date) - interval 0 day)))
group by `b`.`aplicativo`
Table structure
Table aplicativos
CREATE TABLE IF NOT EXISTS `aplicativos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_usuario` int(11) NOT NULL,
`aplicativo` varchar(200) NOT NULL,
`link` varchar(400) NOT NULL,
`quantidade_notificacoes` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;
Table registration
CREATE TABLE IF NOT EXISTS `registration` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`gcm_regid` varchar(300) NOT NULL,
`app_id` int(11) NOT NULL,
`email` varchar(200) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=73876 ;
Here is one approach for MySQL:
SELECT a.aplicativo as PROGRAMA,
sum(Date(r.created_at) = CURDATE()) AS HOJE,
sum(date(r.created_at) = DATE_SUB(CURDATE(), INTERVAL 1 DAY), 1, 0)) AS ONTEM,
...
FROM registration r INNER JOIN
aplicativos a
on r.app_id = a.id
GROUP BY r.app_id ;
Does this give you the expected result?
SELECT
a.aplicativo as PROGRAMA,
COUNT(IF(DATE(r.created_at) = CURDATE(), 1, 0)) AS HOJE,
COUNT(IF(DATE(r.created_at) = DATE_SUB(CURDATE(), INTERVAL 1 DAY), 1, 0)) AS ONTEM,
COUNT(IF(DATE(r.created_at) = DATE_SUB(CURDATE(), INTERVAL 2 DAY), 1, 0)) AS 2DIAS,
COUNT(IF(DATE(r.created_at) = DATE_SUB(CURDATE(), INTERVAL 3 DAY), 1, 0)) AS 3DIAS,
COUNT(IF(DATE(r.created_at) = DATE_SUB(CURDATE(), INTERVAL 4 DAY), 1, 0)) AS 4DIAS,
COUNT(IF(DATE(r.created_at) = DATE_SUB(CURDATE(), INTERVAL 5 DAY), 1, 0)) AS 5DIAS
FROM registration r
INNER JOIN aplicativos a
ON r.app_id = a.id
GROUP BY r.app_id, DATE(r.created_at) with ROLLUP;

MySQL return 0 if query is null

This query should select some statistic values form a table:
(SELECT MAX(`id`) as `count` FROM `stats` LIMIT 1)
UNION (SELECT COUNT(*) as `count` FROM `stats` WHERE DATE(`time`) >= (NOW() - INTERVAL 1 DAY) LIMIT 1)
UNION (SELECT COUNT(*) as `count` FROM `stats` WHERE DATE(`time`) >= (NOW() - INTERVAL 7 DAY) LIMIT 1)
UNION (SELECT COUNT(*) as `count` FROM `stats` WHERE DATE(`time`) >= (NOW() - INTERVAL 30 DAY) LIMIT 1)
UNION (SELECT DATE_FORMAT(MIN(`time`), '%D of %M, %Y') as `count` FROM `stats` LIMIT 1)
I tried this, but it doesn't work
(SELECT MAX(`id`) as `count` FROM `stats` LIMIT 1)
UNION IFNULL((SELECT COUNT(*) as `count` FROM `stats` WHERE DATE(`time`) >= (NOW() - INTERVAL 1 DAY) LIMIT 1), '0')
UNION IFNULL((SELECT COUNT(*) as `count` FROM `stats` WHERE DATE(`time`) >= (NOW() - INTERVAL 7 DAY) LIMIT 1), '0')
UNION IFNULL((SELECT COUNT(*) as `count` FROM `stats` WHERE DATE(`time`) >= (NOW() - INTERVAL 30 DAY) LIMIT 1), '0')
UNION IFNULL((SELECT DATE_FORMAT(MIN(`time`), '%D of %M, %Y') as `count` FROM `stats` LIMIT 1), '0')
You have to do SELECT on IFNULL:
SELECT MAX(`id`) as `count` FROM `stats` LIMIT 1
UNION
SELECT IFNULL((SELECT COUNT(*) as `count`
FROM `stats`
WHERE DATE(`time`) >= (NOW() - INTERVAL 1 DAY) LIMIT 1), 0)
UNION
SELECT IFNULL((SELECT COUNT(*) as `count`
FROM `stats`
WHERE DATE(`time`) >= (NOW() - INTERVAL 7 DAY) LIMIT 1), 0)
UNION
SELECT IFNULL((SELECT COUNT(*) as `count`
FROM `stats`
WHERE DATE(`time`) >= (NOW() - INTERVAL 30 DAY) LIMIT 1), 0)
UNION
SELECT IFNULL((SELECT DATE_FORMAT(MIN(`time`), '%D of %M, %Y') as `count`
FROM `stats` LIMIT 1), 0)
You also have to use 0 instead of '0' and get rid of the parentheses surrounding the first query.

issue combining two mysql queries

I have two queries I am looking to combine into one. This query will be called by backbone in a rails app. The final result should look like this. Thanks in advance.
Title Impressions Completed
Test 1234 500 34
SELECT title, count(*) as impressions FROM `stats`
WHERE `stats`.`user_id` = 2
AND (stats.event ='play' and videopos = '0')
AND (date_time between '2014-03-08 00:00:00' and '2014-03-15 23:59:59')
GROUP BY title;
SELECT title, count(*) as completed FROM `stats`
WHERE `stats`.`user_id` = 2
AND (stats.event ='completed')
AND (date_time between '2014-03-08 00:00:00' and '2014-03-15 23:59:59')
GROUP BY title;
try that :
SELECT title,(select count(*) FROM `stats` WHERE stats.event ='play' and videopos = '0' AND `stats`.`user_id` = 2 AND (date_time between '2014-03-08 00:00:00' and '2014-03-15 23:59:59') ) as impressions ,(select count(*) FROM `stats` WHERE stats.event ='completed' AND `stats`.`user_id` = 2 AND (date_time between '2014-03-08 00:00:00' and '2014-03-15 23:59:59') ) as completed
FROM `stats`
WHERE `stats`.`user_id` = 2
AND (date_time between '2014-03-08 00:00:00' and '2014-03-15 23:59:59')
GROUP BY title;
If you just want to combine the result, use UNION/UNION ALL
SELECT title, count(*) as impressions FROM `stats`
WHERE `stats`.`user_id` = 2
AND (stats.event ='play' and videopos = '0')
AND (date_time between '2014-03-08 00:00:00' and '2014-03-15 23:59:59')
GROUP BY title
UNION
SELECT title, count(*) as completed FROM `stats`
WHERE `stats`.`user_id` = 2
AND (stats.event ='completed')
AND (date_time between '2014-03-08 00:00:00' and '2014-03-15 23:59:59')
GROUP BY title;
Try this :
SELECT title,
Sum(Case When `stats`.`user_id` = 2
AND (stats.event ='play' and videopos = '0')
AND (date_time between '2014-03-08 00:00:00' and '2014-03-15 23:59:59' Then 1 Else 0 End)
as impressions ,
Sum(Case When `stats`.`user_id` = 2
AND (stats.event ='completed')
AND (date_time between '2014-03-08 00:00:00' and '2014-03-15 23:59:59') Then 1 Else 0 End)
as completed
FROM `stats`
WHERE (`stats`.`user_id` = 2
AND (stats.event ='play' and videopos = '0')
AND (date_time between '2014-03-08 00:00:00' and '2014-03-15 23:59:59') )
OR
(`stats`.`user_id` = 2
AND (stats.event ='completed')
AND (date_time between '2014-03-08 00:00:00' and '2014-03-15 23:59:59') )
GROUP BY title;
btw, I am coming from ms-sql-server background but I checked on mysql case when syntax. And I typed in notepad, so untested. But I hope you catch the Sum Case When 1 0 idea and tweak it to your requirement..
I figured it out. This worked for me.
SELECT s.date as date, s.title as title,
count(F.id) as impressions,
count(D.event) as completed
FROM stats s
Left Join stats as F on s.id = F.id and F.event ='play' and F.videopos = '0'
Left Join stats as D on s.id = D.id and D.event = 'completed'
WHERE s.user_id = '2'
AND (s.date_time between '2014-03-08 00:00:00' and '2014-03-18 23:59:59')
AND s.title is not null
GROUP BY s.title

select regular data and insert to average month values as update but error

i want to select regular data from reg_data3 and get average month values and insert into average table as update but it does not work well.
SELECT
year(str_to_date(date, '%Y-%m-%d'))as year,
month(str_to_date(date, '%Y-%m-%d')) as month,
round(avg(evep),2),
round(Avg(sunshine_hrs),2),
round(sum(rainfall),2),
round(AVG(max_temp),2),
round(avg(min_temp),2)
FROM
reg_data3
GROUP BY
year(str_to_date(date, '%Y-%m-%d')),
month(str_to_date(date, '%Y-%m-%d'))
ORDER BY 1 Desc
UPDATE
avg_month_val
set
year='year' ,
month='month',
evep='evep' ,
sunshine_hrs='sun_shine_hrs',
rainfall='rainfall',
max_temp='max_temp',
min_temp='min_temp';
Try this:
UPDATE
avg_month_val
set
`year` = YEAR(STR_TO_DATE(`date`, '%Y-%m-%d')),
`month` = MONTH(STR_TO_DATE(`date`, '%Y-%m-%d')),
`evep` = ROUND(AVG(`evep`), 2),
`sunshine_hrs` = ROUND(AVG(`sunshine_hrs`) ,2),
`rainfall` = ROUND(SUM(`rainfall`), 2),
`max_temp` = ROUND(AVG(`max_temp`), 2),
`min_temp` = ROUND(AVG(`min_temp`), 2)
FROM
reg_data3
GROUP BY
MONTH(STR_TO_DATE(`date`, '%Y-%m-%d')),
MONTH(STR_TO_DATE(`date`, '%Y-%m-%d'))