【问题标题】:How to split amount to months using query?如何使用查询将金额拆分为月份?
【发布时间】:2013-03-30 05:43:46
【问题描述】:

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

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'), N.l-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

【问题讨论】:

  • 更具体,给我们查询您尝试过的数据或表格数据。
  • 这是示例中的数据,我能说得更具体吗?您想要查询我用来获取 id 月份金额 1 2 120 1 6 180 2 2 120 2 6 180 的查询吗?此处太大,无法粘贴。

标签: oracle procedure divide


【解决方案1】:

在 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】:

使用 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!

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多