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 6813447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:34:16+00:00 2026-05-26T20:34:16+00:00

I have a table (foos) which is a list of Foos, one row per

  • 0

I have a table (foos) which is a list of Foos, one row per type of Foo. A second table (items) is a list in which each row is the type of Foo and an amount (and other information). For example, Foo3, 45.2 and Foo2, 12.34.

I’d like to determine the total of the amounts for each type of Foo.

This is my existing code, but there must be a better (more standard or efficient) way:

cursor.execute('''select type from foos''')
foo_types = cursor.fetchall()
results = []
for ft in foo_types:
    cursor.execute('''select sum(amount) from items
        where foo_type =?''', ft)
    results.append((ft, cursor.fetchone()))

How should I code this?

  • 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-26T20:34:17+00:00Added an answer on May 26, 2026 at 8:34 pm
    SELECT foo_type, SUM(amount)
    FROM items
    GROUP BY foo_type
    

    gives you within one query already each foo_type and the corresponding sum. You can build a dictionary from it and use it to expand data from the first query.

    Or put everything into one query:

    cursor.execute("SELECT foo_type, SUM(amount) "
                   "FROM items, foos "
                   "WHERE items.foo_type = foos.type "
                   "GROUP BY foo_type")
    results = list(cursor)
    
    # results is a list of tuples: [(type1, sum1), (type2, sum2), ...]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table foo and a table bar, where each foo might have
I have a table Foo where my foos are stored and if I log
We have 3 entities that relate to each other as such: Foo has many
I have table in which I am inserting rows for employee but next time
I have a bag whose structure looks like this: <bag name=foo fetch=select table=table_of_foos> <key
I have two models: Bar HABTM Foo. Now I have bars_foos join table with
I have a Django-created tables in PostgreSQL 8.4 database, where one table extends another.
I have two classes in rails 3.2.6: class Foo in models/foo.rb (is in table
I have a model Foo and a model Bar, both of which has_many :bar_foos
I have a table called foo with columns like the following: id usedId (id

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.