我想创建一个SQL查询,将某些百分比范围内的物品涨价分组。例如

2件增加0至10%

4件增加10至20%

2件增加20至30%

...

目标服务器: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实际上可能是计算百分比变化的子查询。

10-07 23:53