SQLAlchemy 中的分组总计

发布于 2024-12-12 05:54:24 字数 387 浏览 1 评论 0原文

我似乎找不到任何关于此的好的文档。我有一个用户和订单金额列表,我想显示订单金额总计前 10 名的用户。我在创建一个查询来充分提取 SQLAlchemy 中的这些数据时遇到了问题。有更好的方法来解决这个问题吗?

customers, amount = DBSession.query(Order.customer, func.sum(Order.amount).label('totalamount')).\
    group_by(Order.customer).\
    order_by(func.desc(totalamount)).\
    limit(10)

for a, b in zip(customers, amount):
    print a.name, str(amount)

I can't seem to find any good documentation on this. I have a list of users and order amounts, and I want to display the users with the top 10 order amount totals. I've been having trouble creating a query that sufficiently extracts this data in SQLAlchemy. Is there a better way to approach this?

customers, amount = DBSession.query(Order.customer, func.sum(Order.amount).label('totalamount')).\
    group_by(Order.customer).\
    order_by(func.desc(totalamount)).\
    limit(10)

for a, b in zip(customers, amount):
    print a.name, str(amount)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一指流沙 2024-12-19 05:54:24
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
import random

Base= declarative_base()

class Customer(Base):
    __tablename__ = 'customer'
    id = Column(Integer, primary_key=True)
    name = Column(Unicode)
    orders = relationship("Order", backref="customer")

class Order(Base):
    __tablename__ = "order"

    id = Column(Integer, primary_key=True)
    customer_id= Column(Integer, ForeignKey('customer.id'))
    amount = Column(Integer)

e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)

session = Session(e)

session.add_all([
    Customer(name="c%d" % i, orders=[
        Order(amount=random.randint(10, 100))
        for j in xrange(random.randint(0, 5))
    ])
    for i in xrange(100)
])

amount_sum = func.sum(Order.amount).label('totalamount')
amount = session.query(Order.customer_id, amount_sum).\
            group_by(Order.customer_id).\
            order_by(amount_sum.desc()).\
            limit(10).\
            subquery()

for a, b in session.query(Customer, amount.c.totalamount).\
    join(amount, amount.c.customer_id==Customer.id):
    print a.name, b

有关该模式的一些指南位于 http://www.sqlalchemy.org /docs/orm/tutorial.html#using-subqueries,但总体上首先从 SQL 开始。

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
import random

Base= declarative_base()

class Customer(Base):
    __tablename__ = 'customer'
    id = Column(Integer, primary_key=True)
    name = Column(Unicode)
    orders = relationship("Order", backref="customer")

class Order(Base):
    __tablename__ = "order"

    id = Column(Integer, primary_key=True)
    customer_id= Column(Integer, ForeignKey('customer.id'))
    amount = Column(Integer)

e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)

session = Session(e)

session.add_all([
    Customer(name="c%d" % i, orders=[
        Order(amount=random.randint(10, 100))
        for j in xrange(random.randint(0, 5))
    ])
    for i in xrange(100)
])

amount_sum = func.sum(Order.amount).label('totalamount')
amount = session.query(Order.customer_id, amount_sum).\
            group_by(Order.customer_id).\
            order_by(amount_sum.desc()).\
            limit(10).\
            subquery()

for a, b in session.query(Customer, amount.c.totalamount).\
    join(amount, amount.c.customer_id==Customer.id):
    print a.name, b

some guidelines on the pattern here are at http://www.sqlalchemy.org/docs/orm/tutorial.html#using-subqueries, but overall start in SQL first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文