Python 需要将包含count列的SQL语句转换为SQL Alchemy

Python 需要将包含count列的SQL语句转换为SQL Alchemy,python,sql,sqlalchemy,flask-sqlalchemy,Python,Sql,Sqlalchemy,Flask Sqlalchemy,我提出了以下问题 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 wher

我提出了以下问题

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;
具体地说,我对正在进行计数的非标准列有意见。目前,我有一个大致的测试(基于拼凑一些例子)


但这当然给了我错误。你知道我应该如何正确构建SQL炼金术吗?

“但这当然会给我带来错误”——这些错误消息可能包含有用的信息。他们怎么说?请阅读。
Query.count()
执行查询并返回整数。我猜您的错误是AttributeError:“int”对象没有“label”之类的属性。“非标准列”是标量子查询。请参见如何形成它们,例如:,
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()