使用分析函数LAG("MONTH", 12, 0)做同期比较,但是某些月份缺失怎么办? 本帖最后由 Lyu_KingT 于 2011-01-20 11:16:08 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1 with t as ( 2 select trunc(hire_date,'mm') hire_month, count(employee_id) cnt 3 from employees 4 group by trunc(hire_date,'mm') 5 ) 6 select to_char(hire_month,'yyyy-mm') hire_month,cnt this_year, 7 nvl((select cnt from t where t1.hire_month-interval '12' month=hire_month),0) last_year 8* from t t1 order by hire_monthSQL> /HIRE_MO THIS_YEAR LAST_YEAR------- ---------- ----------2001-01 1 02002-06 4 02002-08 2 02002-12 1 02003-05 2 02003-06 1 42003-07 1 02003-09 1 02003-10 1 02004-01 2 02004-02 2 02004-03 1 02004-05 1 22004-06 1 12004-07 1 12004-08 1 02004-10 1 12005-01 3 22005-02 2 22005-03 6 12005-04 1 02005-06 2 12005-07 2 12005-08 4 12005-09 3 02005-10 3 12005-11 1 0HIRE_MO THIS_YEAR LAST_YEAR------- ---------- ----------2005-12 2 02006-01 3 32006-02 3 22006-03 5 62006-04 3 12006-05 1 02006-06 1 22006-07 3 22006-08 1 42006-09 1 32006-11 2 12006-12 1 22007-01 1 32007-02 3 32007-03 3 52007-04 1 32007-05 2 12007-06 2 12007-08 1 12007-10 1 02007-11 2 22007-12 3 12008-01 4 12008-02 3 32008-03 2 32008-04 2 153 rows selected. 回复:xman_78tom谢谢测试成功了,请问,分析函数里面有相应的实现办法吗? oracle条件语句中的一个问题 请教关于oracle10g增加新的表空间问题 求教一个关于动态数据库的设计 如何让存储过程在没月的一号自动执行 怎样配置ORACLE 的DATA GUARD呀? 紧急求救,大量送分! 关于两个用户之间的权限问题 oracle OEM建立老是失败,怎么用sql语句建job呢 oracle中join的用法 运行oracle对电脑的最底配置? 存储过程下载!! T-SQL转PL-SQL,输出结果也要类似 insert /*+ append */ into select如何使用啊?
1 with t as (
2 select trunc(hire_date,'mm') hire_month, count(employee_id) cnt
3 from employees
4 group by trunc(hire_date,'mm')
5 )
6 select to_char(hire_month,'yyyy-mm') hire_month,cnt this_year,
7 nvl((select cnt from t where t1.hire_month-interval '12' month=hire_month),0) last_year
8* from t t1 order by hire_month
SQL> /HIRE_MO THIS_YEAR LAST_YEAR
------- ---------- ----------
2001-01 1 0
2002-06 4 0
2002-08 2 0
2002-12 1 0
2003-05 2 0
2003-06 1 4
2003-07 1 0
2003-09 1 0
2003-10 1 0
2004-01 2 0
2004-02 2 0
2004-03 1 0
2004-05 1 2
2004-06 1 1
2004-07 1 1
2004-08 1 0
2004-10 1 1
2005-01 3 2
2005-02 2 2
2005-03 6 1
2005-04 1 0
2005-06 2 1
2005-07 2 1
2005-08 4 1
2005-09 3 0
2005-10 3 1
2005-11 1 0HIRE_MO THIS_YEAR LAST_YEAR
------- ---------- ----------
2005-12 2 0
2006-01 3 3
2006-02 3 2
2006-03 5 6
2006-04 3 1
2006-05 1 0
2006-06 1 2
2006-07 3 2
2006-08 1 4
2006-09 1 3
2006-11 2 1
2006-12 1 2
2007-01 1 3
2007-02 3 3
2007-03 3 5
2007-04 1 3
2007-05 2 1
2007-06 2 1
2007-08 1 1
2007-10 1 0
2007-11 2 2
2007-12 3 1
2008-01 4 1
2008-02 3 3
2008-03 2 3
2008-04 2 153 rows selected.