DECODE函数

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')

 


版权声明:本文为weixin_40074861原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。