sql - Divide data from other days by data from one particular day -


i bit stuck on 1 problem few hours now.

let`s have table following data:

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 

i need ratio of say, day 05/05/2012 results first day of month e.g. outstanding of05/05/2012 divided outstanding 01/05/2012 (24 678 956/35 678 956)

what function should use? tried doing on partition / result of to_char(trunc(trunc(a.date_,'mm'), 'mm'),'dd-mm-yyyy')

didnt seem work me

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

the first_value analytic function picks first record partition after doing order by

select month       ,outstanding       ,outstanding/(first_value(outstanding)              on (partition to_char(month,'mm')               order month         )) ratio temp order month; 

output

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 rows selected.

sqlfiddle link


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -