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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:54:33+00:00 2026-06-05T05:54:33+00:00

I hope this question has not been asked before. I have a csv file

  • 0

I hope this question has not been asked before. I have a csv file containing below columns and info and I can upload this to sqlite3 database with the exact same column names

Company Type Qty Price
ABC      r    5   $$$
ABC      h    9   $$$
ABC      s    10  $$$
DER      a    3   $$$
DER      z    6   $$$
GGG      q    12  $$$
GGG      w    20  $$$

but, how do I get the below result on a csv file? Basically, grouping by the company name and still showing Type, Qty and Price, and at the end of lines showing the total qty for each company.

Company Type Qty Price Total Qty
ABC     r    5   $$$      
        h    9   $$$
        s    10  $$$      24
DER     a    3   $$$
        z    6   $$$      9
GGG     q    12  $$$
        w    20  $$$      32

I have been trying with GROUP BY, but no luck.
Any help is much appreciated
Thank you

  • 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-06-05T05:54:35+00:00Added an answer on June 5, 2026 at 5:54 am

    I would do this using a combination of SQL and itertools.groupby.

    For example, something like:

    results = cxn.execute("SELECT * FROM orders ORDER BY Company")
    for company, orders_iter in itertools.groupby(results, key=lambda r: r[0]):
        orders = list(orders_iter)
        total_qty = sum(order[2] for order in orders)
        ... print out orders ...
    

    Someone with more SQL-foo than I have might be able to come up with a query which would produce exactly the results that you want… But that’s probably unnecessary, especially given that, with some minor modifications (calculating the total while iterating over the orders), the Python code would only need O(n) time and O(1) memory.

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

Sidebar

Related Questions

This question has been asked before somewhat, but I hope mine differs. My situation
I realise this question has been asked before however the previous answers have gotten
I'm not sure if this question has been asked before, but I can't seem
I hope this question has not been asked yet, but I want to know
I hope this question has not been asked, I tried to search for similar
I hope that this question has not been asked elsewhere, but I'm having difficulty
I know this question has been asked here before, but I don't think those
This question has not been asked since Rail 3.0 came out (or I cannot
I'm new to this, so sorry if my question has been asked before. I
Hope this question hasn't been asked before. I've got the following working <!-- 640x920

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.