Use a CASE...WHEN function to indicate that the different categories have different sales trends : CASE « Function « SQL / MySQL






Use a CASE...WHEN function to indicate that the different categories have different sales trends

      
mysql>
mysql> CREATE TABLE duck_sales(
    ->     design_num MEDIUMINT NOT NULL AUTO_INCREMENT,
    ->     design_name CHAR(20),
    ->     winter_sales INT,
    ->     spring_sales INT,
    ->     summer_sales INT,
    ->     fall_sales INT,
    ->     design_category CHAR(13),
    ->     primary key(design_num)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> insert into duck_sales(design_name, winter_sales, spring_sales, summer_sales, fall_sales, design_category)values
    -> ("Duck",1067,200,150,267,"Holiday"),
    -> ("Mac",970,770,531,486,"Profession"),
    -> ("Duckula",53,13,21,856,"Literary"),
    -> ("iPhone",782,357, 168, 250,"Profession"),
    -> ("HTML",589,795, 367, 284,"Holiday"),
    -> ("SQL",953,582, 336, 489,"Literary"),
    -> ("Sherlock_Duck",752,657, 259, 478,"Literary"),
    -> ("XML",67,23, 83 ,543,"Holiday"),
    -> ("Database",673,48,625,52,"Profession");
Query OK, 9 rows affected (0.00 sec)
Records: 9  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql> SELECT design_name AS Name,
    ->    CASE design_category
    ->       WHEN "Holiday" THEN "Seasonal"
    ->       WHEN "Profession" THEN "Bi_annual"
    ->       WHEN "Literary" THEN "Random" END AS "Pattern"
    -> FROM duck_sales;
+---------------+-----------+
| Name          | Pattern   |
+---------------+-----------+
| Duck          | Seasonal  |
| Mac           | Bi_annual |
| Duckula       | Random    |
| iPhone        | Bi_annual |
| HTML          | Seasonal  |
| SQL           | Random    |
| Sherlock_Duck | Random    |
| XML           | Seasonal  |
| Database      | Bi_annual |
+---------------+-----------+
9 rows in set (0.00 sec)

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

   
    
    
    
    
    
  








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.Case without else clause
11.Nested case statement
12.Case with comparison
13.Case when with and operator
14.Provide more meaningful value with case clause
15.Make more complete value with case statement
16.Using case statement to check a range
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.