Anda perlu mengelompokkan berdasarkan ekstrak.
SELECT
SUM(amount)
FROM
transaction
GROUP BY
EXTRACT(MONTH FROM when),
EXTRACT(YEAR FROM when)
Dan jika Anda membutuhkan kolom tersebut, maka
SELECT
EXTRACT(MONTH FROM when) as month,
EXTRACT(YEAR FROM when) as year,
SUM(amount)
FROM
transaction
GROUP BY
month,
year
Tentu saja Anda dapat menambahkan ORDER BY
dan gunakan nama pendek juga:
SELECT
EXTRACT(MONTH FROM when) as month,
EXTRACT(YEAR FROM when) as year,
SUM(amount)
FROM
transaction
GROUP BY
month,
year
ORDER BY
year DESC,
month DESC