I have made the following query
select
id,
name,
(select count(*) from users where company_id=companies.id) as num_candidates,
(select count(*) from users where company_id=companies.id and status=1) as num_evaluating ,
(select count(*) from users where company_id=companies.id and created_at between '9/12/2019' and '9/19/2019') as num_last_week
from companies order by num_candidates DESC limit 10;
Specifically I'm having issue with the non standard columns that are taking a count. Currently I have roughly as a test (based on cobbling some examples together)
num_candidates = db.session.query(models.User.id).filter(models.User.company_id == models.Company.id).count().label("num_candidates")
query = db.session.query(models.Company.id,
models.Company.name,
num_candidates).order_by("num_candidates").all()
But this of course gives me errors. Any ideas how I should properly construct the SQL Alchemy?