如何创建在一定百分比范围内分组的SQL查询 [英] How do I create a SQL query that groups in certain percent range
本文介绍了如何创建在一定百分比范围内分组的SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个SQL查询,将某个百分比范围内的物品涨价分组.例如
I would like to create a SQL query that groups the increase price of items in certain percentage range. For example
2件物品增加0至10%
2 items increased between 0 to 10 %
4件物品增加10%至20%
4 items increased between 10 to 20%
2件物品增加20%至30%
2 items increased between 20 to 30%
...
目标服务器:MySQL
Target Server: MySQL
推荐答案
SELECT SUM(CASE WHEN PercentChange >= 0 AND PercentChange <= 10 THEN 1 ELSE 0) END AS ZeroTen,
SUM(CASE WHEN PercentChange > 10 AND PercentChange <= 20 THEN 1 ELSE 0) END AS TenTwenty,
SUM(CASE WHEN PercentChange > 20 AND PercentChange <= 30 THEN 1 ELSE 0) END AS TwentyThirty
FROM MagicChangeTable
注意:MagicChangeTable
实际上可能是计算百分比变化的子查询.
Note: MagicChangeTable
may in fact be a subquery to calculate the percent change.
这篇关于如何创建在一定百分比范围内分组的SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文