如何在SQL中将某个范围内的计算百分比分组?

时间:2011-10-12 12:13:43

标签: mysql sql

  

可能重复:
  How do I create a SQL query that groups in certain percent range

我有一份报告,其中我想展示在一定范围内增加价格的产品数量。让我们说product1已将其价格提高了2%,product2提高了5%,product3提高了15%,依此类推。我可以计算出这套产品的涨价;

Select (({p2.costPrice} - {p.costPrice})/{p.costPrice} * 100) as PERCENTAGE,{p2.pk}
from {Product as p},{Product as p2}, {CategoryProductRelation as rel}, {Category as c}
where {rel.source} = {c.PK} and {rel.target} = {p.PK}
and {p.code} = {p2.code}
and {p.catalogVersion} = 49 
and {p2.catalogVersion} = 37
and {c.catalogVersion} = 49 

**请注意,此查询是基于MySQL的属性查询语言。*

但是,现在我想根据计算的增加百分比值将它们分组到一定的增加范围。所以对于这种情况,例如:

| Range   | #Products |
| 0-10%   | 2         | 
| 11-20%  | 1         |    

重要的是如何在百分比变化时动态地对它们进行分组。因此,如果在下一次运行百分比product1增加了35%,则会显示以下内容:

| Range   | #Products |
| 0-10%   | 1         | 
| 11-20%  | 1         |  
| 31-40%  | 1         | 

如何以这种方式对值进行分组?

0 个答案:

没有答案