How can i make a query with results sorted by a calculated column? (I can not pre calculate the result in other column to do the sorting!)
Example:
In a RDBMS tables like that:
TABLE products(
product_id INTEGER NOT NULL,
description VARCHAR(255) NOT NULL,
price DECIMAL(5,2) NOT NULL,
qty INTEGER NOT NULL,
);
I can perform this query:
SELECT product_id, (price * qty * {{current_tax}}) AS custom_calc
FROM products
ORDER BY custom_calc DESC;
Im my problem i have a variable, in this case {{current_tax}}. In this way I can not pre calculate the result to do the sorting because the variable can change at any time.
there is an elegant solution to problems like this?
Tanks!
As you can see in the documentation, GQL does not support any arithmetic in the SELECT statement.
GQL is there as a convenience for people coming from SQL background, but the low level datastore API is much more like an indexed k/v store.
If
current_taxis always a positive factor applied onprice * qtyit will not to modify the ordering anyway, so I would recommend storingprice * qtyas a new Product field,total_priceand create a DESC index for it.