How do I create a SQL query that groups in certain percent range

前端 未结 2 1170
粉色の甜心
粉色の甜心 2021-01-27 11:12

I would like to create a SQL query that groups the increase price of items in certain percentage range. For example

2 items increased between 0 to 10 %

4 items i

2条回答
  •  生来不讨喜
    2021-01-27 11:52

    If You want all ranges by 10: Use:

    SELECT (d-10)*10 AS `from` , (d-9)*10 AS `to` , c AS `count` FROM 
       (SELECT DISTINCT(FLOOR((`p`.`new`*10)/(`p`.`old`))) AS d,
          count(1) AS c FROM prices as p GROUP BY 1) AS stats
    

    for such structure:

    CREATE TABLE `prices` (
      `old` int(11) NOT NULL,
      `new` int(11) NOT NULL
    );
    

    edit: >=from and < to

提交回复
热议问题