DECODE的语法:DECODE(value,if1,then1,if2,then2,if3,then3,...,else)
表示如果value 等于if1时,DECODE函数的结果返回then1,...,如果不等于任何一个if值,则返回else。
实例:
计算出2020,2021,2022三年入职的员工总人数和每年入职人数.
select count(empno) total,
count(decode(to_char(HIREDATE,'yyyy'),'2020',1,null)) "2020",
count(decode(to_char(HIREDATE,'yyyy'),'2021',1,null)) "2021",
count(decode(to_char(HIREDATE,'yyyy'),'2022',1,null)) "2022"
from emp
where to_char(HIREDATE,'yyyy') in ('2020','2021','2022')