I have a db that contains prices. I want to be able to get the number for prices within 4 different ranges. I know how to do this with 4 different queries but not sure how to do it with 1 query.
| SitePoint Sponsor |


I have a db that contains prices. I want to be able to get the number for prices within 4 different ranges. I know how to do this with 4 different queries but not sure how to do it with 1 query.


Code:SELECT COUNT(CASE WHEN price < 10.00 THEN 'cheap' ELSE NULL END) AS cheap_count , COUNT(CASE WHEN price >= 10.00 AND price < 20.00 THEN 'modest' ELSE NULL END) AS modest_count , COUNT(CASE WHEN price >= 20.00 AND price < 30.00 THEN 'expensive' ELSE NULL END) AS expensive_count , COUNT(CASE WHEN price >= 30.00 THEN 'ridiculous' ELSE NULL END) AS ridiculous_count FROM prices
Homage to "Spaceballs" for the "ridiculous count" reference
Bookmarks