Infinite_Loop
Infinite_Loop

Reputation: 380

Python Sqlite3 advanced grouping

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

Upvotes: 0

Views: 2939

Answers (1)

David Wolever
David Wolever

Reputation: 154664

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.

Upvotes: 3

Related Questions