3

例如,我简化了查询的结果(选择...从...哪里...):

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

不知道如何通过查询得到这个结果?我应该使用程序吗?如果是这样 - 有什么例子吗?

已编辑:我现在似乎需要:如果给定日期比 sysdate 早一个多月(比如说两个月),那么第一笔金额应该加倍,并且所有拆分都应该在 1 个月内完成。难以解释。例如 ....add_months(to_date('2013-03','yyyy-mm'), Nl-1) as 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
4

3 回答 3

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!

于 2013-04-08T08:26:11.500 回答
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;
于 2013-04-08T08:28:22.380 回答
0

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

如果我对你不完整的问题是正确的,这应该会给你你正在搜索的结果

于 2013-04-08T08:16:01.020 回答