Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL and CASE WHEN with a range of values

Tags:

I have an accounts table and a records table where accounts have multiple records. I would like to break down the account totals by "count of records" range. I.e. Show the breakdown of

Count of Records | Count ========================= 0-25 | 100 25 - 50 | 122 50 - 100 | 300 

Etc.

I am using the following query, but I can't get it to group by "grp" which is what I want, any help on the best way to modify query?

SELECT count(*) as ct,     CASE          WHEN COUNT(*) < 25 THEN '1-25'         WHEN COUNT(*) >= 25 < 50 THEN '25-50'         WHEN COUNT(*) >= 50 < 100 THEN '50-100'         WHEN COUNT(*) >= 100 < 250 THEN '100-250'         WHEN COUNT(*) >= 250 < 500 THEN '250-500'         WHEN COUNT(*) >= 500 < 1000 THEN '500-1000'         ELSE '1000+'     END AS grp     FROM records r,accounts a     WHERE r.account_id=a.id     ORDER BY ct 
like image 406
kickdaddy Avatar asked May 26 '10 13:05

kickdaddy


People also ask

Can you use CASE when in a where clause?

CASE can be used in any statement or clause that allows a valid expression. For example, you can use CASE in statements such as SELECT, UPDATE, DELETE and SET, and in clauses such as select_list, IN, WHERE, ORDER BY, and HAVING.

How do I find the range of a record in MySQL?

You have to use the LIMIT clause in the SELECT query. MySQL allows you to set two parameters for the clause, the offset (first parameter) and the number of rows to fetch (second parameter). SELECT * FROM `ABC` LIMIT 0, 100 SELECT * FROM `ABC` LIMIT 100, 100 SELECT * FROM `ABC` LIMIT 200, 100 -- etc...

Can we use CASE statement in where clause in MySQL?

Generally speaking, you can use the CASE expression anywhere that allows a valid expression e.g., SELECT , WHERE and ORDER BY clauses.


1 Answers

try this:

SELECT count(*) as ct,  CASE       WHEN COUNT(*) < 25 THEN '1-25'      WHEN COUNT(*) >= 25 < 50 THEN '25-50'      WHEN COUNT(*) >= 50 < 100 THEN '50-100'      WHEN COUNT(*) >= 100 < 250 THEN '100-250'      WHEN COUNT(*) >= 250 < 500 THEN '250-500'      WHEN COUNT(*) >= 500 < 1000 THEN '500-1000'      ELSE '1000+'  END AS grp  FROM records r, accounts a  WHERE r.account_id=a.id  GROUP BY r.account_id, a.id,      CASE       WHEN COUNT(*) < 25 THEN '1-25'      WHEN COUNT(*) >= 25 < 50 THEN '25-50'      WHEN COUNT(*) >= 50 < 100 THEN '50-100'      WHEN COUNT(*) >= 100 < 250 THEN '100-250'      WHEN COUNT(*) >= 250 < 500 THEN '250-500'      WHEN COUNT(*) >= 500 < 1000 THEN '500-1000'      ELSE '1000+' END ORDER BY count(*) 

You have to "define" the "buckets" you wish to aggregate the original data rows into... This is what the Group By clause is for... It defines the criteria by which each row in the base tables will be analyzed to determine which "bucket" it's data will be aggregated into... The expression or expressions defined in the group by clause are the "definitions" for those buckets.

As the query processes the original data rows, any row for which the value(s) of this expression(s) are the same as an existing bucket is aggregated into that bucket... Any new row with a value not represented by an existing bucket causes a new bucket to be created...

like image 60
Charles Bretana Avatar answered Oct 22 '22 18:10

Charles Bretana