Thank you Tim, that worked perfectly in sqlite, and I imagine it will work well in postgres as well. My server is down right now, so I can't say for sure, but I will definitely try this out.
Thanks again, J On Jun 30, 3:10 pm, Tim Chase <django.us...@tim.thechases.com> wrote: > > Here's my query, (I'm using the stable release version of django, > > otherwise I'd use "annotate" which I found is present in the > > development version). > > > contest = Participant.objects.extra(select={ > > 'contest_stats': """ > > SELECT SUM(amt) > > FROM contest_data > > WHERE contest_data.participant_id = region_participant.id > > """ > > }).order_by('-contest_stats') > > I'm not sure if both postgresql and sqlite support the coalesce > function, but you might try > > contest = Participant.objects.extra(select={ > 'contest_stats': """ > Coalesce(( > SELECT SUM(amt) > FROM contest_data > WHERE contest_data.participant_id = > region_participant.id > ), 0) > """ > }).order_by('-contest_stats') > > (for clarity, that's > > Coalesce((SELECT...), 0) > > which, if the SELECT returns a null result, should then fall to > the next value passed, i.e. 0. > > -tim --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---