Python 使用SQLAlchemy计算结果集中包含的值的实例

Python 使用SQLAlchemy计算结果集中包含的值的实例,python,mysql,orm,sqlalchemy,Python,Mysql,Orm,Sqlalchemy,如果问题标题不具有描述性/措辞不当,则表示歉意 我希望能够计算满足特定条件的行中出现了多少特定值的实例。考虑下面两个表,队列< /C>和 QuealeEngult 队列表: +----+---------+ | id | name | +----+---------+ | 1 | queue A | | 2 | queue B | | 3 | queue C | +----+---------+ +-----+----------+--------+ | id | queue_i

如果问题标题不具有描述性/措辞不当,则表示歉意

我希望能够计算满足特定条件的行中出现了多少特定值的实例。考虑下面两个表,<代码>队列< /C>和<代码> QuealeEngult

队列表:

+----+---------+
| id |  name   |
+----+---------+
|  1 | queue A |
|  2 | queue B |
|  3 | queue C |
+----+---------+
+-----+----------+--------+
| id  | queue_id | foo_id |
+-----+----------+--------+
|  1  |        1 |    10  |
|  2  |        1 |    11  |
|  3  |        1 |    12  |
|  5  |        2 |    20  |
|  6  |        2 |    21  |
|  7  |        2 |    23  |
|  8  |        2 |    24  |
|  9  |        3 |    10  |
|  10 |        3 |    11  |
|  11 |        3 |    20  |
|  12 |        3 |    30  |
+-----+----------+--------+
队列内容表:

+----+---------+
| id |  name   |
+----+---------+
|  1 | queue A |
|  2 | queue B |
|  3 | queue C |
+----+---------+
+-----+----------+--------+
| id  | queue_id | foo_id |
+-----+----------+--------+
|  1  |        1 |    10  |
|  2  |        1 |    11  |
|  3  |        1 |    12  |
|  5  |        2 |    20  |
|  6  |        2 |    21  |
|  7  |        2 |    23  |
|  8  |        2 |    24  |
|  9  |        3 |    10  |
|  10 |        3 |    11  |
|  11 |        3 |    20  |
|  12 |        3 |    30  |
+-----+----------+--------+
当我查询
queue_id==3

+----------+------------+-------------+-----------------------+
| queue_id | queue_name | total_count | contained_in_this_one |
+----------+------------+-------------+-----------------------+
|        1 | queue A    |           3 |                     2 |
|        2 | queue B    |           4 |                     1 |
+----------+------------+-------------+-----------------------+
我不知道如何计算
queue\u contents.foo\u id中出现的
foo\u id
实例,其中queue\u contents.queue\u id==3

+----------+------------+-------------+-----------------------+
| queue_id | queue_name | total_count | contained_in_this_one |
+----------+------------+-------------+-----------------------+
|        1 | queue A    |           3 |                     2 |
|        2 | queue B    |           4 |                     1 |
+----------+------------+-------------+-----------------------+
获取每个队列的
total\u count
非常简单,但是当涉及到设置子查询和条件时,我被难住了。我觉得解决方案包括使用一个子查询并计算该子查询中出现的
foo\u id
s的数量,但我无法让它工作。我不会包括我尝试过的以下查询的maaaany迭代,尽管这会让您了解我所处的轨道:

foo\u id
s在此查询中
如果
queue_id
组不包含重复的
foo_id
项,则可以使用左连接:

qc2 = aliased(QueueContent)

session.query(QueueContent.queue_id,
              func.count(),
              func.count(qc2.foo_id)).\
    outerjoin(qc2, and_(qc2.queue_id == 3,
                        qc2.foo_id == QueueContent.foo_id)).\
    filter(QueueContent.queue_id != 3).\
    group_by(QueueContent.queue_id)
如果是,则可以使用包装为NULLIF(或转换为整数并求和)的EXISTS子查询表达式:

这两种变体都使用这样一个事实:COUNT(expression)生成表达式值不为NULL的行数