mysql - How to sum up these grouped rows that also have an order by -
imagine data in table:
amount/ number /type 100/ 1.2 / 120 / 1.2 /a 130/ 1.1 / 90 / 0.3 / 50/ 2.4 / b 150 / 1.9 /b 150 / 1.9 / b
i want data in 2 groups 1 type a, 1 type b. within these 2 groups want them ordered price, ascending 1 , descending other. can do:
(select * `table` `type`='a' group `number` limit 10) union (select * `table` `type`='b' group `number` limit 15) order `type`), (case when `type`='a' `number` end) asc, (case when `type`='b' `number` end) desc";
however problem want add types of same number.. 1.2 220/1.2/a, instead query 100/1.2/a... how add numbers amounts of same amount , same type?
thanks
this query want:
(select sum(amount) amount, number, type `table` `type`='a' group `number` limit 10 ) union (select sum(amount) amount, number, type `table` `type`='b' group `number` limit 15 ) order `type`, (case when `type`='a' `number` end) asc, (case when `type`='b' `number` end) desc;
as note: return 10 rows of , 15 rows of b after group by
. i'm guessing want.
also, group by
automatically order number
. if want random order, add group rand()
each subquery before limit
.
Comments
Post a Comment