Order by not returning expected results

I am using order by in below query which returns me unexpected results, i want to have order by Revenue

SELECT SUM(flag1+flag2) AS Revenue, @curRank := @curRank + 1 AS rank FROM operations_history, (SELECT @curRank := 0) r WHERE (status=‘Transferred’ OR status=‘Sale Agreed’) AND date LIKE(‘2014-09%’) GROUP BY mar_ref ORDER BY Revenue desc

First value of Revenue desc is 81,000 but its rank is 4 where as i am expecting rank to be 1 as it is the first value.


ORDER BY Revenue is what you want

perhaps your GROUP BY is messing things up? because you’re grouping on a column that you cannot see (it’s not in your SELECT list)

p.s. ~please~ don’t use LIKE on DATEs – LIKE is for CHARs and VARCHARs