python - SQLAlchemy - count status is true -
i have 2 tables in db. 1 named company, other named company_map. company table like:
c_id name contact 1 12334 2 b 12335 3 c 12336 4 d 12337 5 e 12338
company_map table like:
m_id c_id status 1 1 true 2 1 false 3 1 true 4 3 true 5 3 true
i need count number status true in company_map table, group c_id. example, need number of c_id 1 , status true, should 2. can number of c_id now, use func.count(company_map.c_id), 3. how count status == true()? try method, none work.
i got idea sqlalchemy func.count on boolean column
.having(func.count(case([(company_map.status, 1)])) < func.count(company_map.c_id))
if sqlalchemy latest version, can use
.having(func.count(1).filter(company_map.status))
which cleaner old.
Comments
Post a Comment