OK, sorted using: object=pricelist.pricelisttable_set.extra(where=["""(abs(width - %s)) = ( SELECT Min(Abs(pl.width - %s)) FROM table_pricelisttable pl where name_id=%s)"""], params=[w, w,d.pricelist_id])[0]
The error was missing in subquery where name_id=%s. Now it works exactly as should. On 7/31/07, Michael <[EMAIL PROTECTED]> wrote: > Yes, you've right. > > If I put = the query will return None ! > The only query which return what i want is only raw sql: > > select min(abs(width-240)) as m,width from table_pricelisttable where > name_id = 1 group by width order by m limit 1; > than performing the second query to get object. > > like: > cursor = connection.cursor() > cursor.execute("select min(abs(width-%s)) as m, width from > table_pricelisttable where name_id = %s group by width order by m > limit 1;" , [w,pricelist.id]) > row = cursor.fetchone() > base=pricelisttable_set.get(width=row[1]) > > > On 7/30/07, Tim Chase <[EMAIL PROTECTED]> wrote: > > > > > Seems to work now with: > > > w=240 > > > pricelisttable_set.extra(where=["""(table_pricelisttable.width - %s) > > > >= ( SELECT Min(Abs(pl.width - %s)) FROM table_pricelisttable pl) > > > """], params=[w, w])[0] > > > > I would be very surprised if it works now as described with ">=" > > rather than "=". It's basically asking for anything where the > > difference between the target and the width is > > greater-than-or-equal to the minimum difference. Thus, this > > looks like it would return your whole dataset. It sounded like > > you were only interested in those PriceList items where the width > > was closest to (equal to) the minimum difference. > > > > -tim > > > > > > > > > > > > > > > > > > > > > -- > -- > Michael > -- -- Michael --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---