On Mon, Aug 3, 2009 at 6:54 PM, Russell Keith-Magee<freakboy3...@gmail.com> wrote: > To achieve this in SQL, you need to add an inner query for each > summary column you want
this is what i'm doing: q = Order.objects.all().extra ( select={ 'num_items':'SELECT COUNT(*) FROM OT_item WHERE order_id=OT_order.id', 'num_notyet' :'SELECT COUNT(*) FROM OT_item WHERE order_id=OT_order.id AND status=1', 'num_proc' :'SELECT COUNT(*) FROM OT_item WHERE order_id=OT_order.id AND status=2', 'num_ready' :'SELECT COUNT(*) FROM OT_item WHERE order_id=OT_order.id AND status_id=3',}) seems reasonably portable SQL (not that i plan to switch DBs), and lines up nicely in the editor, so any typos are visible enough. It's not my usual style on SQL; but it's the server's job to optimize it. It looks like it's doing the exact same fetches as the equivalent JOINs tks a lot -- Javier --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---