Bo's Oracle Station

查看: 1227|回复: 0

decode的一些用法和AVG空值问题:

[复制链接]

1005

主题

1469

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
12012
发表于 2020-9-10 20:48:50 | 显示全部楼层 |阅读模式
  1. select  * from employees;

  2. select count(*) from employees where to_char(hire_date,'YYYY')=2001
  3. union all
  4. select count(*) from employees where to_char(hire_date,'YYYY')=2002;

  5. select  to_char(hire_date,'YYYY'),  count(*)  from employees
  6.   group by to_char(hire_date,'YYYY') order by 1;
  7. --------
  8. select    sum(decode (  to_char(hire_date,'YYYY'),  2001, 1, 0) )  "Year2001",
  9.              sum(decode (  to_char(hire_date,'YYYY'),  2002, 1, 0) )  "Year2002",
  10.              sum(decode (  to_char(hire_date,'YYYY'),  2003, 1, 0) )  "Year2003",
  11.               sum(decode (  to_char(hire_date,'YYYY'),  2004, 1, 0) )  "Year2004",
  12.              sum(decode (  to_char(hire_date,'YYYY'),  2005, 1, 0) )  "Year2005",
  13.              sum(decode (  to_char(hire_date,'YYYY'),  2006, 1, 0) )  "Year2006",
  14.               sum(decode (  to_char(hire_date,'YYYY'),  2007, 1, 0) )  "Year2007",
  15.              sum(decode (  to_char(hire_date,'YYYY'),  2008, 1, 0) )  "Year2008"
  16.   from employees;
复制代码
以下两个计算平均提成,错误的例子:

  1. select sum(commission_pct)/count(*) from employees;
复制代码
  1. SELECT AVG(NVL(commission_pct, 0))  from
  2. employees;
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Bo's Oracle Station   

GMT+8, 2024-5-3 05:26 , Processed in 0.027870 second(s), 24 queries .

快速回复 返回顶部 返回列表