python - SQLAlchemy 过滤掉跨多列不明显的行

标签 python postgresql sqlalchemy

假设我在表中有这样的数据:

id | other_id | category | amount
--------------------------------
1  | abc      | widget   | 100
2  | abc      | widget   | 200
3  | def      | widget   | 100
4  | ghi      | gadget   | 100
5  | ghi      | gadget   | 100
6  | jkl      | gadget   | 100
7  | jkl      | gadget   | 100

我要查询这张表返回

(other_id, category, sum_of_amount)

其中 sum_of_amount 是同一 other_id 中所有行的 amount 列的总和。除此之外,我想排除 categorysum_of_amount 的组合不唯一的元组。所以查询应该返回元组:

(abc, widget, 300)
(def, widget, 100)

而不是任何 gadget 行,因为组合 (gadget, 200) 不是唯一的。

到目前为止,我有这个:

with session_scope() as db_session:
  query = db_session.query(
    ModelClass.other_id,
    ModelClass.category,
    label('sum_of_amount', func.sum(ModelClass.amount))
  ).group_by(
    ModelClass.other_id,
    ModelClass.category
  )

这个查询没有过滤掉任何东西。我想我需要以某种方式使用 distinct,但我想不通。

最佳答案

您可以使用您记下的查询生成 (ohter_id, category, sum_of_amount) 的结果集::

=# SELECT other_id, category, SUM(amount) AS sum_of_amount
FROM test
GROUP BY other_id, category;

 other_id │ category │ sum_of_amount
──────────┼──────────┼───────────────
 abc      │ widget   │           300
 ghi      │ gadget   │           200
 jkl      │ gadget   │           200
 def      │ widget   │           100
(4 rows)

然后,您必须排除 (category, sum_of_amount) 不唯一的行。在上面的结果集中确定每行的唯一性,您可以添加包含具有相同 (category, sum_of_amount) 的行数的新列,如下所示:

=# SELECT other_id, category, SUM(amount) AS sum_of_amount,
COUNT(*) OVER (PARTITION BY category, SUM(amount)) AS duplicates
FROM test
GROUP BY other_id, category;

 other_id │ category │ sum_of_amount │ duplicates
──────────┼──────────┼───────────────┼───────
 ghi      │ gadget   │           200 │     2
 jkl      │ gadget   │           200 │     2
 def      │ widget   │           100 │     1
 abc      │ widget   │           300 │     1
(4 rows)

正如您在上面的演示中看到的,您掌握了决定因素。现在,您可以通过使用 duplicates 列添加 WHERE 子句来生成您想要查找的结果集。由于 WHERE 子句中不允许窗口函数(duplicates 列的 OVER 部分),我们必须在计算金额总和后评估窗口函数的结果,我们有使用子查询。

=# SELECT other_id, category, sum_of_amount
FROM (
  SELECT other_id, category, SUM(amount) AS sum_of_amount,
  COUNT(*) OVER (PARTITION BY category, SUM(amount)) AS duplicates
  FROM test
  GROUP BY other_id, category
) d
WHERE duplicates = 1
ORDER BY other_id, category;

 other_id │ category │ sum_of_amount
──────────┼──────────┼───────────────
 abc      │ widget   │           300
 def      │ widget   │           100
(2 rows)

上述SQL的SQLAlchemy查询表达式可以是:

from sqlalchemy import func, over
sum_of_amount = label('sum_of_amount', func.sum(ModelClass.amount))
duplicates = over(func.count('*'),
                  partition_by=(ModelClass.category, sum_of_amount))
query = db_session.query(
    ModelClass.other_id,
    ModelClass.category,
    sum_of_amount,
    duplicates,
  ).group_by(
    ModelClass.other_id,
    ModelClass.category
  ).from_self(
    ModelClass.other_id,
    ModelClass.category,
    sum_of_amount
  ).filter(duplicates == 1)

关于python - SQLAlchemy 过滤掉跨多列不明显的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28620864/

相关文章:

ruby-on-rails - Rails/pg group_by 两条记录之间的平均值

Python线程在1个循环后卡住

sql - Postgres SQL查询特定元素中的数组文本[]

python - openpyxl 只读取第一个单元格

sql - 选择相邻表中具有多个其他行的行

python - 如何定义表示集合中最新对象的 SQLAlchemy 关系?

python - 带条件的sqlalchemy唯一约束?

sqlite - Sqlalchemy sqlite url 相对于 home 或环境变量

python - 在 Python 中使用相互或循环(循环)导入时会发生什么?

python - 如何在Python中使用正则表达式搜索除以反斜杠的数据