数据库

迷_雾 2017-12-07 11:39:16
zsprt.TEST_DATE_OF_AMOUNT两个字段 date 和 金额,
--算全年每个月的汇总金额, 但季末的那个月是为全季的累计金额
--例如 1,2,3 三个月,1,2 为当月的汇总金额,3月份 为1,2,3累计的金额
...全文
358 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
碧水幽幽泉 2017-12-07
  • 打赏
  • 举报
回复
问题比较简单,以下SQL可以实现:

--1.创建测试表
create table tmp as
select to_date('2017-01-01','yyyy-mm-dd') dtime, 100 money from dual union all
select to_date('2017-01-05','yyyy-mm-dd') dtime, 200 money from dual union all
select to_date('2017-02-01','yyyy-mm-dd') dtime, 400 money from dual union all
select to_date('2017-02-08','yyyy-mm-dd') dtime, 500 money from dual union all
select to_date('2017-03-01','yyyy-mm-dd') dtime, 100 money from dual union all
select to_date('2017-03-11','yyyy-mm-dd') dtime, 200 money from dual union all
select to_date('2017-03-21','yyyy-mm-dd') dtime, 300 money from dual union all
select to_date('2017-04-01','yyyy-mm-dd') dtime, 100 money from dual union all
select to_date('2017-05-01','yyyy-mm-dd') dtime, 100 money from dual union all
select to_date('2017-06-01','yyyy-mm-dd') dtime, 100 money from dual union all
select to_date('2017-07-01','yyyy-mm-dd') dtime, 100 money from dual union all
select to_date('2017-08-01','yyyy-mm-dd') dtime, 100 money from dual union all
select to_date('2017-09-01','yyyy-mm-dd') dtime, 100 money from dual union all
select to_date('2017-10-01','yyyy-mm-dd') dtime, 100 money from dual union all
select to_date('2017-11-01','yyyy-mm-dd') dtime, 100 money from dual union all
select to_date('2017-12-01','yyyy-mm-dd') dtime, 100 money from dual;

--2.SQL实现
select a.dtime,decode(b.dtime,null,a.money,b.money) money
from( select trunc(dtime,'mm') dtime,sum(money) money from tmp group by trunc(dtime,'mm')) a
left join(select dtime,money
from(select dtime,sum(money)over(partition by to_char(dtime,'q')) money,
row_number()over(partition by to_char(dtime,'q') order by trunc(dtime,'mm') desc) rn
from tmp
) where rn = 1
)b
on a.dtime = b.dtime
卖水果的net 2017-12-07
  • 打赏
  • 举报
回复
有图吗?手机看不到图片。 可以发一下测试数据,和你的预期结果。

2,668

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 认证与考试
社区管理员
  • 认证与考试社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧