SQLalchemy 中带有子查询、分组依据、计数和求和函数的高级 SQL 查询
我写了以下查询。
select distinct(table3.*),
(select count(*)
from table2
where table2.cus_id = table3.id) as count,
(select sum(amount)
from table2
where table2.cus_id = table3.id) as total
from table2,
table1,
table3
where table3.id = table2.cus_id
and table2.own_id = table1.own_id;
它查找列的总和以及产生总和的行数以及来自另一个表的一些关联数据。 (如果您认为可以改进,请随意优化)
我需要将其转换为 SQLAlchemy,但不知道从哪里开始。我将不胜感激任何建议。
I have written the following query.
select distinct(table3.*),
(select count(*)
from table2
where table2.cus_id = table3.id) as count,
(select sum(amount)
from table2
where table2.cus_id = table3.id) as total
from table2,
table1,
table3
where table3.id = table2.cus_id
and table2.own_id = table1.own_id;
It finds the sum of a column and the number of rows that produce the sum as well as some associated data from another table. (Feel free to optimise if you think it can be improved)
I need to convert this in to SQLAlchemy but have no idea where to start. I'd appreciate any advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我对您的查询的重写:
抱歉,无法帮助您解决 SQLAlchemy 部分。
Here's my re-write of your query:
Can't help you with the SQLAlchemy part, sorry.