Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Efficient Grouping of Database Query [RSS Feed]

#1 Dec. 27, 2010 19:33:48

g.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Efficient Grouping of Database Query


Hi, I'm trying to render a page with a table of records.

Each entry in the table is a 'Lift' which I need grouped by
weight_class and then only the top result for each lift shown.

So far example if I have something like this in the database:

weight_class | event | weight
132 | bench press | 156
132 | bench press | 165
132 | deadlift | 315
132 | squat | 278
148 | bench press | 225
148 | bench press | 208
148 | squat | 315

I want to render a table on the web-page like:

132lb. Weight Class
132 | bench press | 165
132 | deadlift | 315
132 | squat | 278

148lb. Weight Class
148 | bench press | 225
148 | squat | 315

Obviously I can hack together a pretty ugly method to do this
(separate queries for each weight class & lift) but since that is a
maintenance nightmare... I was wondering if there is an elegant way to
do this using some of django's features?

Is my best bet to just do one large query for all record setting lifts
and then sort them into a dictionary?

Thanks,

Greg Kerr

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 28, 2010 06:30:08

Mayuresh P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Efficient Grouping of Database Query


You could use the "regroup" template tag to achieve this.http://docs.djangoproject.com/en/dev/ref/templates/builtins/#regroup<http://docs.djangoproject.com/en/dev/ref/templates/builtins/#regroup>

Regards,
Mayureshhttp://twitter.com/#!/<http://twitter.com/#!/django_updates>geeroohttp://twitter.com/#!/django_updates--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 28, 2010 19:52:52

Daniel R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Efficient Grouping of Database Query


On Monday, December 27, 2010 7:33:42 PM UTC, greg wrote:
>
> Hi, I'm trying to render a page with a table of records.
>
> Each entry in the table is a 'Lift' which I need grouped by
> weight_class and then only the top result for each lift shown.
>
> So far example if I have something like this in the database:
>
> weight_class | event | weight
> 132 | bench press | 156
> 132 | bench press | 165
> 132 | deadlift | 315
> 132 | squat | 278
> 148 | bench press | 225
> 148 | bench press | 208
> 148 | squat | 315
>
> I want to render a table on the web-page like:
>
> 132lb. Weight Class
> 132 | bench press | 165
> 132 | deadlift | 315
> 132 | squat | 278
>
> 148lb. Weight Class
> 148 | bench press | 225
> 148 | squat | 315
>
> Obviously I can hack together a pretty ugly method to do this
> (separate queries for each weight class & lift) but since that is a
> maintenance nightmare... I was wondering if there is an elegant way to
> do this using some of django's features?
>
> Is my best bet to just do one large query for all record setting lifts
> and then sort them into a dictionary?
>
> Thanks,
>
> Greg Kerr


You just need to use the aggregation features:http://docs.djangoproject.com/en/1.2/topics/db/aggregation/lifts = Lift.objects.values('weight_class',
'event').aggregate(Max('weight'))

This will give you a list of all weight_class and events, with the max
weight for each.
--
DR.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Efficient Grouping of Database Query [RSS Feed]

Board footer

Moderator control

Enjoy the 23rd of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support