SELECT id SUM pt1 amount AS y1 SUM pt2 amount AS y1 FROM payer AS INNE

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
SELECT
p.id,
SUM(pt1.amount) AS y1,
SUM(pt2.amount) AS y1
FROM payer AS p
INNER JOIN tax AS t
ON p.tax_id = t.id
INNER JOIN tax_category AS tc
ON t.category_id = tc.id
LEFT JOIN payment AS pt1
ON pt1.payer_id = p.id
AND EXTRACT(YEAR from pt1.date) == 1999
AND EXTRACT(MONTH from pt1.date) == 2
ON pt2.payer_id = p.id
AND EXTRACT(YEAR from pt2.date) == 2000
AND EXTRACT(MONTH from pt2.date) == 2
GROUP BY p.id;