假如目前我们有一张表
SQL> select * from a ;
ONE TWO
1 1
1 2
3 9
2 3
此时,我们要将表中ONE为1,2,3的,在统计结果中显示成为A,B,C,TWO中也作相应处理,同时,显示出ONE列内容,那么:
SQL> select one, case one when 1 then ‘A’ when 2 then ‘B’ else ‘C’ end A , case TOW when 1 then ‘C ’ when 2 then ‘B’ else ‘D’ end B from a;
ONE A B
1 A C
1 A B
3 C D
2 B D
上面的方法有什么用呢?其实上面的方法可以用来统计出例如学生成绩,如果>=80分是优秀等。
还可以用上sum函数,来统计ONE为1数目:
SQL> select sum( case when one <2 then 1 else 0 end) A from a ;
A
2
SQL>