我现在有点困在一个问题上几个小时了。
假设我有一张包含以下数据的表格:
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
我需要获得比赛,比如说05/05/2012结果到那个月的第一天 例如。 2012年05月05日的优异成绩除以优异成绩01/05/2012(24 678 956/35 678 956)
我应该使用什么功能? 尝试通过to_char(trunc(a.date _,'MM'),'MM'),'DD-MM-YYYY')的结果进行分区;
似乎没有为我工作
答案 0 :(得分: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 link
答案 1 :(得分: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