日期:2014-05-16  浏览次数:20935 次

请教一个增长率的SQL,谢谢
SELECT T.FEE_MONTH,
  T.MI_CODE,
  SUM(NVL(T.AMNT, 0)) AMNT, /*费用*/
  SUM(NVL(T.TIMES, 0)) TIMES, /*人次*/
  SUM(NVL(T.PCOUNT, 0)) PCOUNT /*人数*/
  FROM M_ZF_INSTFEE_MSTAT T
  WHERE T.PSNTYPE = '1' /*城镇居民*/
  AND T.FEE_MONTH BETWEEN '201012' AND '201112'
  AND (T.MI_CODE = '00000004' OR T.MI_CODE = '05110004')
  GROUP BY T.MI_CODE, T.FEE_MONTH
显示结果如下:
  FEE_MONTH MI_CODE AMNT TIMES PCOUNT
1 201012 00000004 0 0 0
2 201012 05110004 0 0 0
3 201101 00000004 20326 5 5
4 201101 05110004 3328 1 1
5 201102 00000004 2503 2 2
6 201102 05110004 0 0 0
7 201103 00000004 3672 1 1
8 201103 05110004 0 0 0
9 201104 00000004 0 0 0
10 201104 05110004 0 0 0
11 201105 00000004 0 0 0
12 201105 05110004 0 0 0
我想得到如下结果,请教SQL,谢谢!
  FEE_MONTH MI_CODE AMNT TIMES PCOUNT PRE_AMNT(上一月份的金额)
1 201012 00000004 0 0 0 0
2 201012 05110004 0 0 0 0
3 201101 00000004 20326 5 5 0
4 201101 05110004 3328 1 1 0
5 201102 00000004 2503 2 2 20326
6 201102 05110004 0 0 0 3328
7 201103 00000004 3672 1 1 2503
8 201103 05110004 0 0 0 0
9 201104 00000004 0 0 0 3672
10 201104 05110004 0 0 0 0
11 201105 00000004 0 0 0 0
12 201105 05110004 0 0 0 0


------解决方案--------------------
求上一个月用 lag(金额字段) over(order by 日期字段)
------解决方案--------------------
探讨

给你写一个简单的示例

SQL code

with tbl as
(
select '201101' as sdate, '001' as id, 11 as price from dual
union all
select '201101' as sdate, '002' as id, 12 as price from dual
union all
……