Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 544387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:38:57+00:00 2026-05-13T10:38:57+00:00

Using SQLAlchemy , I have a one to many relation with two tables –

  • 0

Using SQLAlchemy, I have a one to many relation with two tables – users and scores. I am trying to query the top 10 users sorted by their aggregate score over the past X amount of days.

users:  
  id  
  user_name  
  score  

scores:  
  user   
  score_amount  
  created  

My current query is:

 top_users = DBSession.query(User).options(eagerload('scores')).filter_by(User.scores.created > somedate).order_by(func.sum(User.scores).desc()).all()  

I know this is clearly not correct, it’s just my best guess. However, after looking at the documentation and googling I cannot find an answer.

EDIT:
Perhaps it would help if I sketched what the MySQL query would look like:

SELECT user.*, SUM(scores.amount) as score_increase 
FROM user LEFT JOIN scores ON scores.user_id = user.user_id 
WITH scores.created_at > someday 
ORDER BY score_increase DESC
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:38:58+00:00Added an answer on May 13, 2026 at 10:38 am

    The single-joined-row way, with a group_by added in for all user columns although MySQL will let you group on just the “id” column if you choose:

        sess.query(User, func.sum(Score.amount).label('score_increase')).\
                   join(User.scores).\
                   filter(Score.created_at > someday).\
                   group_by(User).\
                   order_by("score increase desc")
    

    Or if you just want the users in the result:

    sess.query(User).\
               join(User.scores).\
               filter(Score.created_at > someday).\
               group_by(User).\
               order_by(func.sum(Score.amount))
    

    The above two have an inefficiency in that you’re grouping on all columns of “user” (or you’re using MySQL’s “group on only a few columns” thing, which is MySQL only). To minimize that, the subquery approach:

    subq = sess.query(Score.user_id, func.sum(Score.amount).label('score_increase')).\
                      filter(Score.created_at > someday).\
                      group_by(Score.user_id).subquery()
    sess.query(User).join((subq, subq.c.user_id==User.user_id)).order_by(subq.c.score_increase)
    

    An example of the identical scenario is in the ORM tutorial at: http://docs.sqlalchemy.org/en/latest/orm/tutorial.html#selecting-entities-from-subqueries

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 381k
  • Answers 381k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It seems from the question that what's failing is the… May 14, 2026 at 10:18 pm
  • Editorial Team
    Editorial Team added an answer Your QuoteDetailPartID is an IDENTITY field - you cannot insert… May 14, 2026 at 10:18 pm
  • Editorial Team
    Editorial Team added an answer I have no experience with NAudio, but the exception you… May 14, 2026 at 10:18 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.