On Sat, 2007-05-12 at 16:12 +0000, checco wrote:
> I have a table containing the fields "original_price" and
> "discounted_price". I want to order the results of an SQL statement
> according to the relative discount which is: (original_price-
> discounted_price)/original_price.
> 
> So I wrote the following SQL statement:
> SELECT id, name FROM offers_offer ORDER BY (original_price-
> discounted_price)/original_price
> and found out it was not sorting them properly.
> I then tried directly in sqlite3 and found out that the operation
> (original_price-discounted_price)/original_price was actually
> returning 0 whenever the prices (original and discounted) were
> integers even tough in the database they are defined as floats. The
> sorting was correct when I was putting a decimal price in any of the
> two fields.
> 
> Any suggestion on how to get around this problem?

The usual solution is to multiply something in that equation by 1.0 to
force it to be a float.

> 
> Maybe this could be solved using the django api instead of the
> standard django.db.connection? 

No. Calculated ordering columns is not supported in the Django API at
the moment. It might become easier with some changes we are making in
the future (refactoring QuerySet), but it's not possible at the moment.

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to