gpt4 book ai didi

oracle - 如何使用查询将金额拆分为月份?

转载 作者:行者123 更新时间:2023-12-02 09:57:21 30 4
gpt4 key购买 nike

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

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

最佳答案

在 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;

关于oracle - 如何使用查询将金额拆分为月份?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15874082/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com