Using case statement to check a range : CASE « Function « SQL / MySQL

Home
SQL / MySQL
1.Aggregate Functions
2.Backup Load
3.Command MySQL
4.Cursor
5.Data Type
6.Database
7.Date Time
8.Engine
9.Event
10.Flow Control
11.FullText Search
12.Function
13.Geometric
14.I18N
15.Insert Delete Update
16.Join
17.Key
18.Math
19.Procedure Function
20.Regular Expression
21.Select Clause
22.String
23.Table Index
24.Transaction
25.Trigger
26.User Permission
27.View
28.Where Clause
29.XML
PostgreSQL
MySQL Tutorial
SQL / MySQL » Function » CASE 
Using case statement to check a range
      
mysql>
mysql> CREATE   TABLE PENALTIES
    ->         (PAYMENTNO      INTEGER      NOT NULL,
    ->          EmployeeNO       INTEGER      NOT NULL,
    ->          PAYMENT_DATE   DATE         NOT NULL,
    ->          AMOUNT         DECIMAL(7,2NOT NULL,
    ->          PRIMARY KEY    (PAYMENTNO)          );
Query OK, rows affected (0.00 sec)

mysql>
mysql> INSERT INTO PENALTIES VALUES (1,  6'1980-12-08',100);
Query OK, row affected (0.00 sec)

mysql> INSERT INTO PENALTIES VALUES (244'1981-05-05', 75);
Query OK, row affected (0.00 sec)

mysql> INSERT INTO PENALTIES VALUES (327'1983-09-10',100);
Query OK, row affected (0.00 sec)

mysql> INSERT INTO PENALTIES VALUES (4,104'1984-12-08', 50);
Query OK, row affected (0.00 sec)

mysql> INSERT INTO PENALTIES VALUES (544'1980-12-08', 25);
Query OK, row affected (0.00 sec)

mysql> INSERT INTO PENALTIES VALUES (6,  8'1980-12-08', 25);
Query OK, row affected (0.00 sec)

mysql> INSERT INTO PENALTIES VALUES (744'1982-12-30', 30);
Query OK, row affected (0.00 sec)

mysql> INSERT INTO PENALTIES VALUES (827'1984-11-12', 75);
Query OK, row affected (0.00 sec)

mysql>
mysql>
mysql> SELECT   PAYMENTNO, AMOUNT,
    ->          CASE
    ->             WHEN AMOUNT >= AND AMOUNT <= 40
    ->                THEN 'low'
    ->             WHEN AMOUNT >= 41 AND AMOUNT <= 80
    ->                THEN 'moderate'
    ->             WHEN AMOUNT >= 81
    ->                THEN 'high'
    ->             ELSE 'incorrect' END AS CATEGORY
    -> FROM     PENALTIES;
+-----------+--------+----------+
| PAYMENTNO | AMOUNT | CATEGORY |
+-----------+--------+----------+
|         100.00 | high     |
|         |  75.00 | moderate |
|         100.00 | high     |
|         |  50.00 | moderate |
|         |  25.00 | low      |
|         |  25.00 | low      |
|         |  30.00 | low      |
|         |  75.00 | moderate |
+-----------+--------+----------+
rows in set (0.00 sec)

mysql>
mysql> drop table PENALTIES;
Query OK, rows affected (0.00 sec)

mysql>

   
    
    
    
    
    
  
Related examples in the same category
1.CASE() Function
2.The next version of the CASE() function
3.CASE Branching
4.CASE is a variant of IF that is useful when all the branch decisions depend on the value of a single expression.
5.Using case in select statement
6.Use both case expressions shown previously in a SELECT statement.
7.Using the comparison value form of CASE...WHEN
8.Using CASE...WHEN for evaluating conditions
9.Variants of CASE.WHEN with no ELSE clause
10.Use a CASE...WHEN function to indicate that the different categories have different sales trends
11.Case without else clause
12.Nested case statement
13.Case with comparison
14.Case when with and operator
15.Provide more meaningful value with case clause
16.Make more complete value with case statement
17.Use case else to check the exceptions
18.Using case with comparison
19.Case clause and aggregate function
20.If ELSE is omitted, the null value is returned.
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.