简体   繁体   English

获取前n条记录,占每个组的> = 90%

[英]Get top n records accounting for >= 90% amount for each group

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. 我最近已在这里阅读并实现了对类似问题的答案,并想看看是否可以再进一步。

Link to original question 链接到原始问题

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: 使用下面列出的代码(感谢上一个问题的@Bluefeet),结果是这样的:

+--------+-------+---+
| 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. 我想做的是按组,按金额描述选择ID,直到满足该组的> = 90%贡献。 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). 请注意,对于每个组,每个组的总和如何为90(在此示例中,每个组的总和为100)。

Thanks in advance, and let me know if there's more information that may be of help. 在此先感谢您,如果有更多信息可以帮助您,请告诉我。

Presumably you mean 90% of the sum of "amount". 大概是指“金额”总和的90%。 Start by doing a cumulative sum of amount : 首先做一个累加的amount

  select id, `group`, amount,
         @cumsum := if(@group = `group`, @cumsum + amount, 0) as cumsum,
         @group := `group` as dummy
  from mytable
  order by `Group`, amount desc, id

Then, you need the total for each group for the 90% calculation. 然后,您需要每个组的总计来进行90%计算。 Let's do this by joining in this query to an aggregation query: 让我们通过将此查询加入聚合查询来做到这一点:

select id, `group`, amount, cumsum
from (select id, `group`, amount,
             @cumsum := if(@group = `group`, @cumsum + amount, 0) as cumsum,
             @group := `group` as dummy
      from mytable
      order by `Group`, amount desc, id
     ) t join
     (select `group`, sum(amount) as tot
      from mytable
      group by `group`
     ) tg
     on t.`group` = tg.`group`
where cum <= 0.9 * tot;

Or, if you want the first value after 90%, then use: 或者,如果您希望第一个值 90%之后,请使用:

where (cumsum - amount) < 0.9 * tot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM