Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlalchemy group_by and sum

I have a SiteStaff table that I want to group all the staff together with the same staffId and add up the holiday column.

>     group =  session.query(SiteStaff, func.sum(SiteStaff.Holiday)).group_by(SiteStaff.StaffID).all()
>     
>     print group

The output groups the staff together but does not add the column.

Here is the sql that i am trying to mimic:

UPDATE Staff p, (SELECT StaffID, SUM(Holiday) as mysum
 FROM SiteStaff GROUP BY StaffID) as s

    SET p.TotalDaysHolidayAllowed = s.mysum
    WHERE p.StaffID = s.StaffID
like image 802
Mantis Avatar asked Sep 02 '26 05:09

Mantis


1 Answers

It was the StaffSite part that stopped it from working. I needed to add each field in like so:

session.query(SiteStaff.StaffID, func.sum(SiteStaff.Holiday)).group_by(SiteStaff.StaffID).all()
like image 124
Mantis Avatar answered Sep 03 '26 18:09

Mantis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!