sql query that groups different items into buckets

Viewed 69960

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
i1        | 2
i2        | 12
i3        | 4
i4        | 16
i5        | 6

output:

range   | number of item
0 - 10  |  3
10 - 20 |  2

The way I am doing it so far is

SELECT count(*)
FROM my_table
Where price >=0
and price <10

then

SELECT count(*)
FROM my_table
Where price >=10
and price <20

and then copy pasting my results each time into excel.

Is there an automatic way to do this in an sql query?

5 Answers

Here's a simple mysql solution. First, calculate the bucket index based on the price value.

select *, floor(price/10) as bucket from mytable
+------+-------+--------+
| name | price | bucket |
+------+-------+--------+
| i1   |     2 |      0 |
| i2   |    12 |      1 |
| i3   |     4 |      0 |
| i4   |    16 |      1 |
| i5   |     6 |      0 |
+------+-------+--------+

Next, group by the bucket index and calculate sum of price and format the prange column.

select concat(bucket * 10, '-', (bucket * 10 - 1) + 10) as prange, count(price)
from  (select *, floor(price/10) as bucket from mytable) t1 
group by bucket;
+--------+--------------+
| prange | count(price) |
+--------+--------------+
| 0-9    |            3 |
| 10-19  |            2 |
+--------+--------------+

Using decode in orderBy:

select
case when price >= 0 and price < 10    then "  0 - 10"
           when price > 10 and price <= 50   then " 10+ - 50"
           when price > 50 and price <= 100  then " 50+ - 100"
           else "over 100"
end As PriceRange,
count(item_name) as ItemTotal,
customer_name,
cust_address,
cust_sigid
from YourTable
group by 
case when price >= 0 and price < 10    then "  0 - 10"
       when price > 10 and price <= 50   then " 10+ - 50"
       when price > 50 and price <= 100  then " 50+ - 100"
       else "over 100"
end
order by
decode((case when price >= 0 and price < 10    then "  0 - 10"
           when price > 10 and price <= 50   then " 10+ - 50"
           when price > 50 and price <= 100  then " 50+ - 100"
           else "over 100"
end),"  0 - 10", 1, " 10+ - 50", 2, " 50+ - 100", 3, "over 100", 4, 5) ASC;

Decode works in a fashion in which it takes expression in the first argument and then subsequent key-value pairs and then finally a default value.

Documentation:

https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions040.htm

Related