Http://www.cnblogs.com/eshizhan/archive/2012/04/06/2435493.html
1. Two forms of the case-when expression:
--Simple case function case sex when ' 1 ' then ' Male ' when ' 2 ' Then ' Women ' Else ' other ' END --case search function casewhen sex = ' 1 ' Then ' man ' when sex = ' 2 ' Then ' women ' else ' other ' END
2. When the case is used in different locations of the SQL statement:
2.1 Select Case
SELECT grade, count (case if sex = 1 then 1 /*sex 1 for boys, 2 girls * /ELSE NULL END) Number of boys, count (case when s ex = 2 then 1 ELSE NULL END) Number of schoolgirls from students GROUP by grade;
2.2 where case
SELECT t2.*, t1.* from T1, T2 WHERE (case when t2.compare_type = ' A ' and T1. Some_type like ' nothing% ' then 1 when t2.compare_type! = ' A ' and T1. Some_type ' nothing% ' then 1 ELSE 0 END) = 1
2.3 Group by case
SELECT case when salary <= "1" when salary > Salary <= and "2" when salary > Salary <= 3 ' when salary > Salary <= and ' 4 ' ELSE NULL END Salar Y_class,--alias name Count (*) from table_a GROUP by case when salary <= and ' 1 ' when salary &G T and salary <= 2 ' when salary > Salary <=, then ' 3 ' when salary > Salary <= and 4 '
3. Other implementations of If-then-else
3.1 Decode function:
Select decode (Sex, ' M ', ' Male ', ' F ', ' Female ', ' Unknown ') from employees;
Use of case when in Oracle