如何使用查询将金额拆分为几个月?

时间:2013-04-08 08:00:14

标签: oracle procedure divide

我有例如简化的查询结果(select ... from ... where ...):

id months amount
1  2     120
1  6      180
2  2     120
2  6      180

现在我需要从上面的第一个表中得到另一个表,它应该是这样的:

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

不知道如何通过查询获得此结果?我应该使用程序吗?如果是这样 - 任何例子?

EDITED: 我现在似乎需要:如果在sysdate之前给出的日期超过一个月(比如说两个月)那么第一个金额应该加倍,并且所有拆分应该在1个月内完成。难以解释。例如.... add_months(to_date('2013-03','yyyy-mm'),N.l-1)作为month_name ...服用sysdate是2013-04,然后结果应该出现:

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

3 个答案:

答案 0 :(得分:3)

使用Oracle 11,您可以执行递归查询来获取它。

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;

例如 http://sqlfiddle.com/#!4/27edc/1

10g建模变体:

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

我添加了rownum以获得一个独特的地址检测系统,因为您的数据具有非唯一ID!

答案 1 :(得分:3)

在9i及更高版本中,您可以:

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;

答案 2 :(得分:0)

你的问题似乎不完整。我想你只是想通过删除Where条件来从表中获取数据。所以请尝试使用“SELECT * FROM YOUR_TABLE_NAME”

如果我对您的问题不正确,这应该会为您提供您正在搜索的结果