PostgreSQL
 sql >> Base de données >  >> RDS >> PostgreSQL

La jointure chaînée ne filtre pas comme prévu

J'ai réalisé que j'avais besoin d'une sous-sélection comme celle-ci :

SELECT 
  UPPER(TO_CHAR(d.start_date, 'mon')) AS month,
  EXTRACT(MONTH FROM d.start_date) AS month_num,
  SUM(t.cost_planned) FILTER (WHERE t.aasm_state IN ('open', 'planned' ) ) AS planned,
  SUM(t.cost_actual)  FILTER (WHERE t.aasm_state = 'closed') AS actual
FROM GENERATE_SERIES('2020-01-01'::date, '2020-12-01'::date, '1 month') d(start_date)
LEFT JOIN
(
SELECT t.*
FROM activity_tasks t
LEFT JOIN activities a
ON a.id = t.activity_id
WHERE a.type = 'My Type'
) AS t ON t.start_date >= d.start_date and t.start_date < d.start_date + '1 month'::interval
GROUP BY d.start_date
ORDER BY d.start_date