Reputation: 793
I have, for example simplified, this result from query (select... from... where...):
id months amount
1 2 120
1 6 180
2 2 120
2 6 180
Now I need to get, from first table above, ANOTHER table and it should look like this:
id months month_name amount
2 120
1 2 2013-04 60
1 2 2013-05 60
6 180
1 6 2013-04 30
1 6 2013-05 30
1 6 2013-06 30
1 6 2013-07 30
1 6 2013-08 30
1 6 2013-09 30
2 120
2 2 2013-04 60
2 2 2013-05 60
6 180
2 6 2013-04 30
2 6 2013-05 30
2 6 2013-06 30
2 6 2013-07 30
2 6 2013-08 30
2 6 2013-09 30
No idea how to get this result by query? Should I use procedure? If so - any examples?
EDITED: It seems I need now: If given date is more than a month (lets say two months) before sysdate then the first amount should be doubled and all split should be done in 1 month less. Hard to explain. For example ....add_months(to_date('2013-03','yyyy-mm'), N.l-1) as month_name... taking sysdate is 2013-04, then result should appear:
id months month_name amount
2 120
1 2 2013-03 120
6 180
1 6 2013-03 60
1 6 2013-04 30
1 6 2013-05 30
1 6 2013-06 30
1 6 2013-07 30
2 120
2 2 2013-03 60
6 180
2 6 2013-03 60
2 6 2013-04 30
2 6 2013-05 30
2 6 2013-06 30
2 6 2013-07 30
Upvotes: 3
Views: 897
Reputation: 17643
In 9i and higher versions you can:
select decode(flag, 'original', null, id), months, amount
from(
select id,
months,
add_months(to_date('2013-04','yyyy-mm'), N.l-1) as month_name,
amount/months as amount,
'splitted' as flag
from your_table t
join (select level l from dual connect by level < 1000) N
on (t.months >= N.l)
union all
select id, months, amount, null, 'original'
from your_table
)
order by id, months;
Upvotes: 3
Reputation: 21973
with Oracle 11 you can do a recursive query to get it.
with base_Query as (select * from test),
data (id, months, amount, iter, month_amount)
as (select id, months, amount, 0, amount/months
from base_Query
union all
select id, months, amount, iter+1, month_amount
from data
where iter+1 <= months)
select case when iter = 0 then to_number(null) else id end id,
months,
case when iter = 0 then amount else month_amount end amount
from data d
order by d.id, months;
eg http://sqlfiddle.com/#!4/27edc/1
10g modelling variant too:
with base_query as (select rownum r, t.*, amount/months monthly_amount from test t)
select case i when 0 then to_number(null) else id end id,
months, amount
from base_query
model
partition by (r)
dimension by (0 as i)
measures (months, amount, monthly_amount, id)
rules (
id[for i from 0 to months[0] increment 1] = id[0],
amount[any] = case cv(i) when 0 then amount[0] else monthly_amount[0] end,
months[any] = months[0]
);
http://sqlfiddle.com/#!4/27edc/4
i added rownum to get a unique addresssing system, as your data had non-unique id's!
Upvotes: 3
Reputation: 196
Your question seems incomplete. I think you just wanted the data from table by removing the Where condition. So please try with "SELECT * FROM YOUR_TABLE_NAME"
This should give you the result you were searching for if I am correct with your incomplete question
Upvotes: 0