The main problem is to count different elements with Sqlalchemy at the same time. I want to achieve the following SQL.
SELECT
metrics_stages.job_id,
COUNT(*) FILTER(WHERE metrics_stages.stage = 'Submitted') AS submitted_count,
COUNT(*) FILTER(WHERE metrics_stages.stage = 'Applied') AS applied_count,
COUNT(distinct metrics_stages.talents_job_id) FILTER(WHERE metrics_stages.if_interview = true) AS interview_count
FROM metrics_stages
GROUP BY metrics_stages.job_id
But i can not make it possible.