I want to sort product by discount on certain condition
ORDER BY CASE WHEN @OrderBy = 0 THEN table.id END ASC, CASE WHEN @Orderby = 2 THEN tabl
Don't calculate the discount in the ORDER BY clause but in the SELECT
ORDER BY
SELECT
SELECT *, (100-(table.price/table.oldprice))*100 as discount FROM table
...
ORDER BY CASE WHEN @OrderBy = 0 THEN table.id END ASC, CASE when @orderby=2 THEN table.id END ASC, CASE WHEN @OrderBy = 4 THEN discount END ASC