1

I have a table storing weatherdata in 5min intervals.

Now I need result with total raindays grouped by Year like this:.

2010 100 raindays
2011 120 raindays
2013 90  raindays

This is my current query:

SELECT date, SUM(rain) FROM $tableName  GROUP BY date HAVING SUM(rain) > 0"; 

as expected, this gives me following result:

2010-01-02 1.2 (mm)
2010-01-05 1.6 (mm)
2010-02-10 2.6 (mm)

How I have to change my query, to have this grouped by year(date) and counted days ?

Thanks all for your help

PM

4

2 回答 2

1

You can group by YEAR(date), to sum the rain grouped by year. Then to count the number of days you can COUNT DISTINCT the days without the time part, using DATE(date) function.

SELECT YEAR(date), SUM(rain), COUNT(DISTINCT DATE(date)) days
FROM $tableName
WHERE rain>0
GROUP BY YEAR(date)

Please see fiddle here.

于 2013-03-30T11:11:07.130 回答
0

The following query might help you if you want to find maximum element is each group,

select 
    e.date, e.rain
from
    TableName e
        join
    (select 
        date, MAX(rain) MS
    from
        TableName
    group by date
    Having rain > 0) m ON e.date = M.date and e.rain = m.rain
于 2013-03-30T11:17:24.360 回答