关于sql报表查询语句求解的问题。

灬风一样的男子灬 2013-02-01 09:22:48

问题是这样的,需求是需要统计出20111231和20121231每个金额段的信息。例如:客户A在20111231是4W元低于5W并且在20121231还是低于5W为3W那属于第一个区间,则如图客户数加1,那么资产变化则为-1W后面以此类推,我写了下sql,但是觉得这样比较麻烦,并且只能查询出一个区间段的。
select count(*) as 人数,sum(b.qian1-a.qian1) from (
select distinct(cusno) as ren1 ,sum(depmdbbal) as qian1 from plat where datadate = 20111231 and depmdbbal<50000 group by cusno ) as a join
(select distinct(cusno) as ren1 ,sum(depmdbbal) as qian1 from plat where datadate = 20121231 and depmdbbal<50000 group by cusno) as b on a.ren1=b.ren1
求高手指点,谢谢。
...全文
411 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
a89723253 2013-03-13
  • 打赏
  • 举报
回复
select q, count(decode(w.q, 1, 1, 0)), sum(decode(w.q, 1, nvl(w1.depmdbbal, 0) - w.depmdbbal , 0)), count(decode(w.q, 2, 1, 0)), sum(decode(w.q, 2, nvl(w1.depmdbbal, 0) - w.depmdbbal , 0)), count(decode(w.q, 3, 1, 0)), sum(decode(w.q, 3, nvl(w1.depmdbbal, 0) - w.depmdbbal , 0)), count(decode(w.q, 4, 1, 0)), sum(decode(w.q, 4, nvl(w1.depmdbbal, 0) - w.depmdbbal , 0)), count(decode(w.q, 5, 1, 0)), sum(decode(w.q, 5, nvl(w1.depmdbbal, 0) - w.depmdbbal , 0)), count(decode(w.q, 6, 1, 0)), sum(decode(w.q, 6, nvl(w1.depmdbbal, 0) - w.depmdbbal , 0)), count(decode(w.q, 7, 1, 0)), sum(decode(w.q, 7, nvl(w1.depmdbbal, 0) - w.depmdbbal , 0)) from (select cusno, sum(depmdbbal) depmdbbal, case when sum(depmdbbal) < 50000 then 1 when sum(depmdbbal) >= 50000 and sum(depmdbbal) < 200000 then 2 when sum(depmdbbal) >= 200000 and sum(depmdbbal) < 500000 then 3 when sum(depmdbbal) >= 500000 and sum(depmdbbal) < 2000000 then 4 when sum(depmdbbal) >= 2000000 and sum(depmdbbal) < 5000000 then 5 when sum(depmdbbal) >= 5000000 and sum(depmdbbal) < 8000000 then 6 else 7 end q from qian1 t where t.datadate = 20111231 group by cusno) w, (select select cusno, sum(depmdbbal) depmdbbal case when sum(depmdbbal) < 50000 then 1 when sum(depmdbbal) >= 50000 and sum(depmdbbal) < 200000 then 2 when sum(depmdbbal) >= 200000 and sum(depmdbbal) < 500000 then 3 when sum(depmdbbal) >= 500000 and sum(depmdbbal) < 2000000 then 4 when sum(depmdbbal) >= 2000000 and sum(depmdbbal) < 5000000 then 5 when sum(depmdbbal) >= 5000000 and sum(depmdbbal) < 8000000 then 6 else 7 end q from qian1 t1 where t1.datadate = 20121231 group by cusno) w1 where w.cusno = w1.cusno(+) group by w1.q order by q
hnwenxia 2013-03-05
  • 打赏
  • 举报
回复
select 客户ID,sum(金额), case when 金额 <50000 then '5W以下' when 金额 >=50000 and 金额<=100000 then '5W-10W' end 区间, count(客户ID) from table group by 客户ID,case when 金额 <50000 then '5W以下' case when 金额 >=50000 and 金额<=100000 then '5W-10W' end 区间
yuppy 2013-02-03
  • 打赏
  • 举报
回复
你sum的时候其实可以case when 一下的。就可以弄出多个区间的。 sum(case when xx >5 and xx < 20 then depmdbbal else null end) 类似这样。 建议。

17,090

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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