Grouping by day, month, or year is pretty simple in MySQL. However, how do you group by quarter? Luckily MySQL comes with the very helpful Quarter() function. See example below.
Here's a simple count of orders this year:
SELECT
COUNT(DISTINCT id)
FROM orders
WHERE CONVERT_TZ(paid_at,'+00:00','-06:00') > '2022-01-01'
The CONVERT_TZ function is putting the timestamp paid_at field into CST from GMT. That gives us this result:
Ok, so how do we get it by quarter? Easy peasy. Just add a year and quarter statement up front, then group by it and order appropriately.
SELECT
YEAR(CONVERT_TZ(paid_at,'+00:00','-06:00')) as year,
QUARTER(CONVERT_TZ(paid_at,'+00:00','-06:00')) as quarter,
COUNT(DISTINCT id)
FROM orders
WHERE CONVERT_TZ(paid_at,'+00:00','-06:00') > '2022-01-01'
GROUP BY
YEAR(CONVERT_TZ(paid_at,'+00:00','-06:00')),
QUARTER(CONVERT_TZ(paid_at,'+00:00','-06:00'))
ORDER BY
YEAR(CONVERT_TZ(paid_at,'+00:00','-06:00')) DESC,
QUARTER(CONVERT_TZ(paid_at,'+00:00','-06:00')) DESC
Which gives us this result:
Booya! All done!