sqlalchemy group by


Re: [sqlalchemy] Simple example of a GROUP BY

Lance Edgar
Mon, 05 Jul 2010 21:24:08 -0700

On 7/5/2010 10:55 PM, Andrew Bialecki wrote:
I've looked for a while, but I can't find the *best* way to use the
ORM to generate the following query:

SELECT user_id, count(*) cnt
FROM orders
GROUP BY user_id

I have classes that represent both User and Order.  So what should go
after:

Order.query.[fill this in]

Sorry this is such an easy question, feel free to forward me to an
example, I just couldn't find one.
I think the following might work?


from sqlalchemy import func

session.query(Order.user_id,  func.count(Order.user_id)).group_by(Order.user_id)
Anyway a little more is to be found here:

http://www.sqlalchemy.org/docs/ormtutorial.html#counting
http://www.sqlalchemy.org/docs/reference/orm/query.html#sqlalchemy.orm.query.Query.group_by

Lance

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

posted on 2010-10-11 21:57  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2010/10/11/1848273.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值