打印本文 打印本文 关闭窗口 关闭窗口
oracle分析函数(二)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2721  更新时间:2009/4/22 22:03:16  文章录入:mintao  责任编辑:mintao
sp;          0
5765      200408          27130.638       26058.46       26301.881               0               0
20 rows selected.

利用lag和lead函数,我们可以在同一行中显示前n行的数据,也可以显示后n行的数据.


4. sum,avg,max,min移动计算数据介绍

计算出各个连续3个月的通话费用的平均数
  1  select area_code,bill_month, local_fare,
  2     sum(local_fare)
  3             over (  partition by area_code
  4                     order by to_number(bill_month)
  5                     range between 1 preceding and 1 following ) "3month_sum",
  6     avg(local_fare)
  7             over (  partition by area_code
  8                     order by to_number(bill_month)
  9                     range between 1 preceding and 1 following ) "3month_avg",
10     max(local_fare)
11             over (  partition by area_code
12                     order by to_number(bill_month)
13                     range between 1 preceding and 1 following ) "3month_max",
14     min(local_fare)
15             over (  partition by area_code
16                     order by to_number(bill_month)
17                     range between 1 preceding and 1 following ) "3month_min"
18  from (
19     select area_code,bill_month,sum(local_fare) local_fare
20     from t
21     group by area_code,bill_month
22* )
SQL> /

AREA_CODE BILL_MONTH       LOCAL_FARE 3month_sum 3month_avg 3month_max 3month_min
--------- ---------- ---------------- ---------- ---------- ---------- ----------
5761      200405            13060.433  26379.363 13189.6815   13318.93  13060.433
5761      200406            13318.930  40089.628 13363.2093  13710.265  13060.433
5761      200407            13710.265  41164.976 13721.6587  14135.781   13318.93
40089.628 = 13060.433 + 13318.930 + 13710.265
13363.2093 = (13060.433 + 13318.930 + 13710.265) / 3
13710.265 = max(13060.433 + 13318.930 + 13710.265)
13060.433 = min(13060.433 + 13318.930 + 13710.265)
5761      200408            14135.781  27846.046  13923.023  14135.781  13710.265
5762      200405            12643.791  25438.851 12719.4255   12795.06  12643.791
5762      200406            12795.060  38663.148  12887.716  13224.297  12643.791
5762      200407            13224.297  39395.825 13131.9417  13376.468   12795.06
5762      200408            13376.468  26600.765 13300.3825  13376.468  13224.297
5763      200405            13060.433  26379.363 13189.6815   13318.93  13060.433
5763      200406            13318.930  40089.628 13363.2093  13710.265  13060.433
5763      200407            13710.265  41164.976 13721.6587  14135.781   13318.93
5763      200408            14135.781  27846.046  13923.023  14135.781  13710.265
5764      200405            12487.791  25782.978  12891.489  13295.187  12487.791
5764      200406            13295.187  39227.071 13075.6903  13444.093  12487.791
5764      200407            13444.093  40668.974 13556.3247  13929.694  13295.187
5764      200408            13929.694  27373.787 13686.8935  13929.694  13444.093
5765      200405            25057.736  51116.196  25558.098   26058.46  25057.736
5765      200406            26058.460  77418.077 25806.0257  26301.881  25057.736
5765      200407            26301.881  79490.979  26496.993  27130.638   26058.46
5765      200408            27130.638  53432.519 26716.2595  27130.638  26301.881

20 rows selected.

5. ratio_to_report函数的介绍




  Quote:
  1  select bill_month,area_code,sum(local_fare) local_fare,
  2     ratio_to_report(sum(local_fare)) over
  3       ( partition by bill_month ) area_pct
  4  from t
  5* group by bill_month,area_code
SQL> break on bill_month skip 1
SQL> compute sum of local_fare on bill_month
SQL> compute sum of area_pct on bill_month
SQL> /

BILL_MONTH AREA_CODE       LOCAL_FARE   AREA_PCT
---------- --------- ---------------- ----------
200405     5761             13060.433 .171149279
           5762             12643.791 .165689431
           5763             13060.433 .171149279
           5764             12487.791 .163645143
           5765             25057.736 .328366866
**********           ---------------- ----------
sum                         76310.184          1

200406     5761             13318.930 .169050772
           5762 &n

上一页  [1] [2] [3] [4]  下一页

打印本文 打印本文 关闭窗口 关闭窗口