I have recently read and implemented the answer to a similar question here and would like to see if it can be taken one step further.
Using a similar sample data set:
+--------+-------+---+
| ID | Group | Amount|
+--------+-------+---+
| 1 | 1 | 50 |
| 2 | 1 | 25 |
| 3 | 1 | 15 |
| 4 | 1 | 10 |
| 5 | 1 | 0 |
| 6 | 1 | 0 |
| 7 | 2 | 60 |
| 8 | 2 | 20 |
| 9 | 2 | 10 |
| 10 | 2 | 5 |
| 11 | 2 | 5 |
| 12 | 2 | 0 |
| 13 | 3 | 90 |
| 14 | 3 | 10 |
+--------+-------+---+
Using the code listed below (Thanks to @Bluefeet from the previous question), the outcome is this:
+--------+-------+---+
| 1 | 1 | 50 |
| 2 | 1 | 25 |
| 7 | 2 | 60 |
| 8 | 2 | 20 |
| 13 | 3 | 90 |
| 14 | 3 | 10 |
+--------+-------+---+
set @num := 0, @group := '';
select id, `group`, amount
from
(
select id, `group`, amount,
@num := if(@group = `group`, @num + 1, 1) as row_number,
@group := `group` as dummy
from mytable
order by `Group`, amount desc, id
) as x
where x.row_number <= 2;
What I would like to do is select the IDs by Group, by Amount desc, until >=90% contribution for the group is met. Ideally, it should look like this:
+--------+-------+---+
| 1 | 1 | 50 |
| 2 | 1 | 25 |
| 3 | 1 | 15 |
| 7 | 2 | 60 |
| 8 | 2 | 20 |
| 9 | 2 | 10 |
| 13 | 3 | 90 |
+--------+-------+---+
Notice how for each group the sum of the amount for each group is 90 (the amounts for each group add up to 100 for this example).
Thanks in advance, and let me know if there's more information that may be of help.