表结构如下
编号  名称  到期日期 延迟天数  付款期限  到期余额
001   联想  07-08-20   -4         30       250.00
001   联想  07-08-10    6         30       300.00
001   联想  07-08-20   -4         30       500.00
002   联众  07-07-05   41         30       100.00
002   联众  07-08-16   0          30       200.00
002   联众  07-05-15   91         30       200.00我想得到的结果如下:编号    名称    合计金额  1个月  2个月 3个月 4个月  5个月  6个月  6个月以上
001     联想     1050     750     300    0     0      0      0       0002     联众     500      200     0      100   0     200     0       0按编号名称进行应付款统计,  延迟小于0天的统计到1个月,大于0天<=30天的统计到第二个月,大于30天<=60天的统计到第三个月,以此类推,得出上以的表结果,此表为一帐龄分析报表,因我对PL/SQL不是很熟,恳请这里的高手们帮我写出这个语句,谢谢了!

解决方案 »

  1.   

    一个语句写出来有难度。行列转换需要嵌套实现。
    类似于:SELECT 编号,名称,VAL,DECODE(延迟月数,1,1),DECODE(延迟月数,2,2),DECODE(延迟月数,3,3)
    FROM (
    select 编号,名称,CASE WHEN 延迟天数 <0 THEN 1
                                   WHEN 延迟天数 >0 AND 延迟天数 <=30 THEN 2
                                   WHEN 延迟天数 >30 AND 延迟天数 <=60 THEN 3
                              ELSE 0
                              END AS 延迟月数
                           ,SUM(到期余额) AS VAL
            FROM 表
            GROUP BY 编号,名称,延迟月数
    )
    只写了3个月的其他自己试着写吧。
      

  2.   

    本人技术比较差,写一个仅供参考
    select 编号,名称, sum(到期余额) as '合计金额',(select sum(f.到期余额) from 表 f group by t.名称 having 延迟天数<0 and f.名称=t.名称) as '1个月',(处理其他月份) from 表 t group by t.名称
      

  3.   

    语句执行到这里,走不通 WHEN 延迟天数 <0 THEN 1
                                   WHEN 延迟天数 >0 AND 延迟天数 <=30 THEN 2
                                   WHEN 延迟天数 >30 AND 延迟天数 <=60 THEN 3
    还有没有别的方法,麻烦给我写出语句!谢谢了!
      

  4.   

    在下是从SQL server转过来做PL/SQL,所以很多PL/SQL的语法结果就弄不出来,还望多多指导一下,改了还是得不到想要的结果,麻烦各位再帮帮我!
      

  5.   

    mantisXF(枫の叶)   非常感谢!!
      

  6.   

    我测试是成功的,你试试看~~~select tt.id,
           tt.custName,
           tt.total_money,
           max(nvl(decode(tt.classical,1,tt.monthmoney),0)) as "1 month",
           max(nvl(decode(tt.classical,2,tt.monthmoney),0)) as "2 month",
           max(nvl(decode(tt.classical,3,tt.monthmoney),0)) as "3 month",
           max(nvl(decode(tt.classical,4,tt.monthmoney),0)) as "4 month",
           max(nvl(decode(tt.classical,5,tt.monthmoney),0)) as "5 month",
           max(nvl(decode(tt.classical,6,tt.monthmoney),0)) as "6 month",
           max(nvl(decode(tt.classical,7,tt.monthmoney),0)) as "above 6 month"
      from (select zz.id,
                  zz.custName,
                  max(zz.ar_total) as total_money,
                  sum(zz.overmoney) as monthmoney,
                  zz.classical
             from (select ar.id,
                         ar.custName,
                         ar.overmoney,
                          sum (ar.overmoney) over(partition by ar.id) ar_total,
                          case when ar.overdays <= 0 then 1
                              when ar.overdays >0 and ar.overdays <=30 then 2
                              when ar.overdays >30 and ar.overdays <=60 then 3
                              when ar.overdays >60 and ar.overdays <=90 then 4
                              when ar.overdays >90 and ar.overdays <=120 then 5
                              when ar.overdays >120 and ar.overdays <=150 then 6
                              when ar.overdays >0 and ar.overdays <=30 then 7
                          end as classical
                    from ar
                  )zz
             group by zz.id,zz.custName,zz.classical
           )tt
      group by tt.id,tt.custName,tt.total_money;
    ========================result=========================        ID CUSTNAME  TOTAL_MONEY    1 month    2 month    3 month    4 month    5 month    6 month above 6 month
    ---------- --------- ----------- ---------- ---------- ---------- ---------- ---------- ---------- -------------
             1 lenovo           1050        750        300          0          0          0          0             0
             2 lianzhong         500        200          0        100          0        200          0             0