oracle比上个月增长率,求一个月增长率的sql

具体如下:

SQL> select * from table1;

MONTH_ID    DEPT_ID   N_NUMBER

---------- ---------- ----------

200906          1        100

200906          2         50

200906          3         40

200907          1        150

200907          2         80

200907          4         60

6 rows selected

SQL>

SQL> select  tmp2.dept_id,

2          nvl(tmp1.increase_rate, -999) increase_rate

3  from

4  (select  month_id,

5           dept_id,

6           increase_rate

7  from

8  (select month_id,

9          dept_id,

10          case

11              when month_id = 200907 and lag(n_number) over(partition by dept_id order by month_id) is null and n_number is not null then

12                   999

13              when month_id = 200907 and lag(n_number) over(partition by dept_id order by month_id) is not null and n_number is not null then

14                   (n_number - lag(n_number) over(partition by dept_id order by month_id)) / lag(n_number) over(partition by dept_id order by month_id)

15          end increase_rate

16  from table1)

17  where month_id = 200907) tmp1,

18  (select distinct dept_id from table1) tmp2

19  where tmp1.dept_id(+) = tmp2.dept_id

20  order by tmp2.dept_id;

DEPT_ID INCREASE_RATE

---------- -------------

1           0.5

2           0.6

3          -999

4           999

SQL>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值