Python Forum
sum() list from SQLAlchemy output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sum() list from SQLAlchemy output
#1
Hello guys,

I'm sure it is simple. Have a SQLAlchemy query such as:
qp = db.session.query(UInventory, func.sum(UInventory.quantity).label('total')).filter(
            UInventory.location_id == s.loc_id, UInventory.userid == current_user.userid).group_by(UInventory.part_id). subq.all()
Later in my code, I want to sum up the result of the 'total' from the query.

for now I'm using this code, which works well, but I'd like to use sum() function instead:

sum = 0
for q in qp:
    sum += q.total
the type of the variable q is <class 'sqlalchemy.engine.row.Row'>

I tried
max_total = sum(q.total) 
but of course I got this error:
Error:
print(sum(qp.total)) AttributeError: 'list' object has no attribute 'total'
How can I achieve something that simple ?

Thank you

Note: I do not want a SQL query solution !
Reply
#2
If have done a bit a research, I found this way, but the performance is quite bad, and I'm still using a loop.

        list = range(10000000)
        starttime = timeit.default_timer()
        for i in list:
            sum1 = 0
            for q in qp:
                sum1 += q.total
        print({timeit.default_timer() - starttime})

        starttime = timeit.default_timer()
        for i in list:
            sum2 = sum([int(q.total) for q in qp])
        print({timeit.default_timer() - starttime})
The sum() is about 2 times slower
Reply
#3
(May-14-2022, 02:37 AM)Personne Wrote: Note: I do not want a SQL query solution !

Why do you want it to be fast but don't want it to be on the SQL side? Python has lots of good attributes, but speedy loops is not one of them.
ndc85430 likes this post
Reply
#4
(May-14-2022, 03:43 AM)bowlofred Wrote:
(May-14-2022, 02:37 AM)Personne Wrote: Note: I do not want a SQL query solution !

Why do you want it to be fast but don't want it to be on the SQL side? Python has lots of good attributes, but speedy loops is not one of them.

For the simple reason that I know how to do it in SQL ! and I'm trying to understand how Python can do something that simple quickly.
Reply
#5
Python can't do it as fast. The simple way would be to import func from sqlalchemy and call func.sum() on the query.

sum() will be the fastest way to do it in python, but constructing the container for sum() to operate on may take longer if the query doesn't just hand over those specific values.
Reply
#6
(May-16-2022, 01:23 AM)bowlofred Wrote: Python can't do it as fast. The simple way would be to import func from sqlalchemy and call func.sum() on the query.

sum() will be the fastest way to do it in python, but constructing the container for sum() to operate on may take longer if the query doesn't just hand over those specific values.

I did a quick benchmark, surprisingly the loop is faster than the SQL query.

Anyway thank for your help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to append to list a function output? rama27 5 6,766 Aug-24-2020, 10:53 AM
Last Post: DeaD_EyE
  json.dumps list output qurr 12 5,263 Apr-08-2020, 10:13 PM
Last Post: micseydel
  If item in list = true, Output = xx kroh 0 1,498 Feb-19-2020, 09:17 AM
Last Post: kroh
  Difference in list output OokaydO 6 3,331 Nov-09-2019, 12:33 AM
Last Post: OokaydO
  output list reducing each time through loop 3Pinter 6 3,530 Mar-19-2019, 01:31 PM
Last Post: perfringo
  os.popen output to a list .. evilcode1 1 5,328 Oct-02-2018, 08:42 PM
Last Post: ODIS
  Converting Raw output into Nested List Nirmal 13 5,731 Aug-12-2018, 08:47 AM
Last Post: Nirmal
  25 blank lines in my sorted_fruits output list! raven61 7 4,292 Aug-09-2018, 11:30 PM
Last Post: raven61
  ciscolib cdp output list printing support anna 3 3,340 Jul-25-2018, 12:18 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020