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


Messages In This Thread
sum() list from SQLAlchemy output - by Personne - May-14-2022, 02:37 AM
RE: sum() list from SQLAlchemy output - by Personne - May-14-2022, 02:50 AM
RE: sum() list from SQLAlchemy output - by Personne - May-15-2022, 08:47 PM
RE: sum() list from SQLAlchemy output - by Personne - May-17-2022, 12:25 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to append to list a function output? rama27 5 9,013 Aug-24-2020, 10:53 AM
Last Post: DeaD_EyE
  json.dumps list output qurr 12 7,146 Apr-08-2020, 10:13 PM
Last Post: micseydel
  If item in list = true, Output = xx kroh 0 1,828 Feb-19-2020, 09:17 AM
Last Post: kroh
  Difference in list output OokaydO 6 4,483 Nov-09-2019, 12:33 AM
Last Post: OokaydO
  output list reducing each time through loop 3Pinter 6 4,538 Mar-19-2019, 01:31 PM
Last Post: perfringo
  os.popen output to a list .. evilcode1 1 6,543 Oct-02-2018, 08:42 PM
Last Post: ODIS
  Converting Raw output into Nested List Nirmal 13 7,420 Aug-12-2018, 08:47 AM
Last Post: Nirmal
  25 blank lines in my sorted_fruits output list! raven61 7 5,519 Aug-09-2018, 11:30 PM
Last Post: raven61
  ciscolib cdp output list printing support anna 3 4,122 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