MySQL Forums
Forum List  »  InnoDB

Re: Grouping Data Into Ranges
Posted by: Jay Pipes
Date: July 12, 2005 04:02AM

Sainandoor,

I believe you are confusing the stored procedure CASE statement with the regular CASE control flow function. In the regular CASE function, you don't use the END CASE, whereas with the stored procedure CASE statement, you do. They serve different purposes. Here is what you would use:

mysql> SELECT * FROM numbers;
+-----------+
| my_number |
+-----------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
+-----------+
7 rows in set (0.00 sec)

mysql> SELECT SUM(CASE WHEN my_number < 5 THEN 1 ELSE 0 END) as lessthan5 FROM numbers;
+-----------+
| lessthan5 |
+-----------+
| 4 |
+-----------+
1 row in set (0.00 sec)

mysql> SELECT SUM(IF(my_number < 5, 1, 0)) as lessthan5 FROM numbers;
+-----------+
| lessthan5 |
+-----------+
| 4 |
+-----------+
1 row in set (0.02 sec)

As you can see, I've shown the use of the SUM(IF()) alternative that you might find slightly more efficient/easier.

Hope this helps,

Jay Pipes
Community Relations Manager, North America, MySQL Inc.

Got Cluster? http://www.mysql.com/cluster
Personal: http://jpipes.com

Options: ReplyQuote


Subject
Views
Written By
Posted
3735
July 11, 2005 07:09PM
Re: Grouping Data Into Ranges
2146
July 12, 2005 04:02AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.