https://i.stack.imgur.com/0yDsf.png
year | month | no_of_people | avg |
---|---|---|---|
2005 | 1 | Overall | 8 |
2005 | 2 | Overall | 5.0 |
2005 | 3 | Overall | 2.7 |
2005 | 4 | Overall | 4.1 |
2005 | 5 | Overall | 6.8 |
2005 | 6 | Overall | 5.2 |
2005 | 7 | Overall | 4.7 |
2005 | 8 | Overall | 4.4 |
2005 | 9 | Overall | 3.8 |
2005 | 10 | Overall | 7 |
2005 | 11 | Overall | 4.9 |
2005 | 12 | Overall | 6.5 |
My issue lies in essentially calculating the avg of three months (123, 456, etc) and displaying this new value as quarterly average of Q1/2/3/4 (indicating Quarters). Sorry for formatting, but an ideal output would be something like:
year | quarter | no_of_people | avg |
---|---|---|---|
2005 | Q1 | Overall | xxx |
2005 | Q2 | Overall | xxx |
Not sure how to even begin with this query and how to group the months into quarters. Any thanks would be very much appreciated!
2
Answers
fiddle
If you have a
date
column in that table, you can directly use MySQL QUARTER() function. However, this will return1-4
instead ofQ1-Q4
. If you don’t have date column, then you can combineyear
andmonth
column plus a01
for "day" value, useDATE_FORMAT
to make it identifiable as date and useQUARTER()
on it:Demo fiddle