Posted By

daipratt on 10/02/10


Tagged

mysql


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

bobbym245


MySQL - Group by price range


 / Published in: MySQL
 

URL: http://daipratt.co.uk/tag/mysql/

Group by user defined price bands.

  1. SELECT price_range, count(*) AS num
  2. FROM
  3. (SELECT CASE WHEN product_price >= 150 AND product_price <= 249.99 THEN '150-249.99'
  4. WHEN product_price >= 250 AND product_price <= 299.99 THEN '250-299.99'
  5. WHEN product_price >= 300 AND product_price <= 399.99 THEN '300-399.99'
  6. WHEN product_price >= 400 AND product_price <= 499.99 THEN '400-499.99'
  7. WHEN product_price >= 500 AND product_price <= 999.99 THEN '500-999.99'
  8. ELSE 'over 1000'
  9. END as price_range
  10. FROM washing_machines
  11. WHERE 1
  12. ) AS price_summaries
  13. GROUP BY price_range

Report this snippet  

You need to login to post a comment.