0

我现在在一个问题上卡住了几个小时。

假设我有一个包含以下数据的表:

month          outstanding
01/05/2012     35 678 956
02/05/2012     33 678 956
03/05/2012     31 678 956
04/05/2012     27 678 956
05/05/2012     24 678 956

我需要得到比方说,2012 年 5 月 5 日结果与该月第一天的 EG 未偿还 05/05/2012 除以 01/05/2012 未偿还(24 678 956/35 678 956)

我应该使用什么功能?尝试按 to_char(trunc(trunc(a.date_,'MM'), 'MM'),'DD-MM-YYYY') 的结果对 / 进行分区

似乎对我不起作用

4

2 回答 2

4
create table temp (month date , outstanding number);
insert into temp values(to_date('01/05/2012','dd/mm/yyyy'),35678956);
insert into temp values(to_date('02/05/2012','dd/mm/yyyy'),33678956);
insert into temp values(to_date('03/05/2012','dd/mm/yyyy'),31678956);
insert into temp values(to_date('04/05/2012','dd/mm/yyyy'),27678956);
insert into temp values(to_date('05/05/2012','dd/mm/yyyy'),24678956);
insert into temp values(to_date('01/06/2012','dd/mm/yyyy'),44678956);
insert into temp values(to_date('02/06/2012','dd/mm/yyyy'),41678956);

FIRST_VALUE分析函数在first record执行完之后从分区中选择ORDER BY

SELECT month
      ,outstanding
      ,outstanding/(FIRST_VALUE(outstanding)
             OVER (PARTITION BY to_char(month,'mm') 
             ORDER BY month
        )) as ratio
FROM temp
ORDER BY month;

输出

MONTH     OUTSTANDING      RATIO
--------- ----------- ----------
01-MAY-12    35678956          1
02-MAY-12    33678956 .943944548
03-MAY-12    31678956 .887889096
04-MAY-12    27678956 .775778193
05-MAY-12    24678956 .691695015
01-JUN-12    44678956          1
02-JUN-12    41678956 .932854295

选择了 7 行。

SQLFIDDLE 链接

于 2012-06-17T14:58:16.430 回答
1

试试这个:

SELECT t1.month,
       t1.outstanding / t2.outstanding o2
FROM your_table t1
INNER JOIN
  (SELECT *
   FROM your_table
   WHERE trunc(MONTH, 'mm') = MONTH) t2 ON trunc(t1.MONTH, 'mm') = t2.MONTH
于 2012-06-17T14:22:45.560 回答