MariaDB Hidden Gem: Create Aggregate Function

Have you ever written a query where the GROUP BY was easy, but the aggregate was the problem?

You know how to group the rows.
You know what result you want for each group.
But none of the built-in aggregate functions really match your logic.

So you end up with a long expression using SUM(), CASE, IF(), GROUP_CONCAT(), JSON functions, or application-side code. It works, but it is not beautiful. And if you need the same logic in several places, it becomes even worse.

This is exactly the kind of problem MariaDB’s CREATE AGGREGATE FUNCTION solves.