【发布时间】:2022-08-14 07:35:39
【问题描述】:
我有两个查询,唯一的区别是一个是计算成功状态,另一个是失败状态。有没有办法在一个查询中得到这个结果?我正在使用 SQLALchemy 进行查询。
success_status_query = (
db_session.query(Playbook.operator, func.count(Playbook.operator).label(\"success\"))
.filter(Playbook.opname != \"failed\")
.join(AccountInfo, AccountInfo.hardware_id == Playbook.hardware_id)
.group_by(Playbook.operator)
)
failure_status_query = (
db_session.query(Playbook.operator, func.count(Playbook.operator).label(\"failure\"))
.filter(Playbook.opname == \"failed\")
.join(AccountInfo, AccountInfo.hardware_id == Playbook.hardware_id)
.group_by(Playbook.operator)
)
-
你可以和
q1.union(q2)做一个简单的联合,但是可能很难区分成功和失败的结果。
标签: python sql sqlalchemy fastapi