Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
group by
#1
Hello,

I just cant get group by to work :(
I followed a tutorial: http://flask-sqlalchemy.pocoo.org/2.1/queries/
...and tried to extend it with group_by() unsuccessfully.
I use
Class.query.filter(Class.name=='John').all()
to get my data and everything works fine.

Now, I want for the data to be grouped by Class.surname...
but it doesn't work if I add
group_by(Class.surname)
i.e.:
Class.query.filter(Class.name=='John').group_by(Class.id, Class.surname).all()

Can someone point me to the right direction, please?
Reply
#2
Group by is used with aggregate functions like count, max, min, sum, avg
It looks more like you want to sort by surname.
Also, you are using id in the group_by  and id is unique for each record, so definitely no place for it here
can you explain what you try to achieve as a result?
Reply


Forum Jump:

User Panel Messages

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