sql >> Database teknologi >  >> RDS >> PostgreSQL

Kædet sammenføjning filtrerer ikke som forventet

Indså, at jeg har brug for et undervalg som dette:

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



  1. Privat chatsystem MYSQL-forespørgsel for at vise sidste besked fra afsender/modtager

  2. Hvad er den lagrede procedure og hvorfor den lagrede procedure?

  3. Har brug for hjælp igen til at deltage i borde

  4. Sådan bruges STRCMP() til at sammenligne 2 strenge i MySQL