sql query that groups different items into buckets

前端 未结 3 834
梦毁少年i
梦毁少年i 2020-12-14 00:34

I am trying to write a query that returns the count of items whose price falls into certrain buckets:

For example if my table is:

item_name | price
i         


        
3条回答
  •  一向
    一向 (楼主)
    2020-12-14 01:26

    You can try grouping by 10 units of price:

    SELECT COUNT(*) AS tally,
           FLOOR(price/10) AS prange,
           CONCAT(10*FLOOR(price/10), "-", 10*FLOOR(price/10)+9) AS rstr
    FROM my_table
    GROUP BY prange;
    

提交回复
热议问题