Lukas Eder notes a bit of ANSI SQL:
The following aggregate function computes the number of rows per group which satifsy the
FILTER
clause:
SELECT
COUNT
(*) FILTER (
WHERE
BOOK.TITLE
LIKE
'A%'
),
COUNT
(*) FILTER (
WHERE
BOOK.TITLE
LIKE
'B%'
),
...
FROM
BOOK
This is useful for pivot style queries, where multiple aggregate values are computed in one go. For most basic types of aggregate function, it can be emulated simply by using
CASE
expressions, because standard aggregate functions ignoreNULL
values when aggregating.
Lukas shows how you can also implement this logic using JSON_TRANSFORM()
though I think I’d just as soon stick with COUNT(CASE WHEN BOOK.TITLE LIKE 'A%' THEN 1 END)
and try hard not to think about shredding JSON.