回答

收藏

如何基于另一列计算接下来的n行的平均值-SQL(Oracle)

技术问答 技术问答 203 人阅读 | 0 人回复 | 2023-09-12

我正着按月计算每个月POLICY_ID平均月保费价值如下所示。当客户将他/她的年付款频率更新为不同于12的值时,我需要手动计算PREMIUM平均月值。如何获得?MONTHLY
9 Y: A7 R! X, h9 O: s' Q" u0 }_PREMIUM_DESIRED中显示的值?提前致谢。6 Q0 x! s# z  Q5 [+ d4 `$ l  x
注意:Oracle版本12c
7 ~4 X- i3 N: O" ]我尝试过的
' }) ~& N4 b& K% aSELECT     T.*,   SUM(PREMIUM) OVER(PARTITION BY T.POLICY_ID ORDER BY T.POLICY_ID,T.PAYMENT_DATE ROWS BETWEEN CURRENT ROW AND 12/T.YEARLY_PAYMENT_FREQ-1 FOLLOWING T.YEARLY_PAYMENT_FREQ) MONTLY_PREMIUM_CALCULATEDFROM MYTABLE T;数据代码:
9 f0 A) l' r$ \' a- J( I3 kDROP TABLE MYTABLE;CREATE TABLE MYTABLE (POLICY_ID NUMBER(11),PAYMENT_DATE DATE,PREMIUM NUMBER(5),YEARLY_PAYMENT_FREQ NUMBER(2),MONTHLY_PREMIUM_DESIRED NUMBER(5);                           INSERT INTO MYTABLE VALUES (1,DATE '2014-10-120、120、120);INSERT INTO MYTABLE VALUES (1,DATE  '2014-11-360,4,120);INSERT INTO MYTABLE VALUES (1,DATE '2014-12-01、0、4、120);INSERT INTO MYTABLE VALUES (1,DATE '2015-01-01、0、4、120);INSERT INTO MYTABLE VALUES (1,DATE '2015-02-360,4,120);INSERT INTO MYTABLE VALUES (1,DATE '2015-03-01、0、4、120);INSERT INTO MYTABLE VALUES (1,DATE '2015-04-01、0、4、120);INSERT INTO MYTABLE VALUES (1,DATE '2015-05-720,2,120);INSERT INTO MYTABLE VALUES (1,DATE '2015-06-01、0、2、120);INSERT INTO MYTABLE VALUES (1,DATE '2015-07-01、0、2、120);INSERT INTO MYTABLE VALUES (1,DATE '2015-08-01、0、2、120);INSERT INTO MYTABLE VALUES (1,DATE '2015-09-01、0、2、120);INSERT INTO MYTABLE VALUES (1,DATE '2015-10-01、0、2、120);INSERT INTO MYTABLE VALUES (1,DATE '2015-11-120、120、120);INSERT INTO MYTABLE VALUES (2,DATE '2015-01-60,3,15);INSERT INTO MYTABLE VALUES (2,DATE '2015-02-01,0,3,15;INSERT INTO MYTABLE VALUES (2,DATE '2015-03-01,0,3,15;INSERT INTO MYTABLE VALUES (2,DATE '2015-04-01,0,3,15;INSERT INTO MYTABLE VALUES (2,DATE '2015-05-180,1,15);INSERT INTO MYTABLE VALUES (2,DATE '2015-06-01、0、1、15);INSERT INTO MYTABLE VALUES (2,DATE '2015-07-01、0、1、15);INSERT INTO MYTABLE VALUES (2,DATE '2015-08-01、0、1、15);INSERT INTO MYTABLE VALUES (2,DATE '2015-09-01、0、1、15);INSERT INTO MYTABLE VALUES (2,DATE '2015-10-01、0、1、15);INSERT INTO MYTABLE VALUES (2,DATE '2015-11-01',0,1,15);INSERT INTO MYTABLE VALUES (2,DATE '2015-12-01、0、1、15);INSERT INTO MYTABLE VALUES (2,DATE '2016-01-01、0、1、15);INSERT INTO MYTABLE VALUES (2,DATE '2016-02-01、0、1、15);INSERT INTO MYTABLE VALUES (2,DATE '2016-03-01、0、1、15);INSERT INTO MYTABLE VALUES (2,DATE '2016-04-01、0、1、15);INSERT INTO MYTABLE VALUES (2,DATE '2016-05-15、12、15;INSERT INTO MYTABLE VALUES (2,DATE '2016-06-01',15,12,15);SELECT * FROM MYTABLE;编辑:
* x, \& L( ~1 n( ~无论付款频率如何,客户都可以更改PREMIUM金额。下面,对POLICY_ID = 1,我加了从
  c' Y0 c% c7 ~9 S, v. @0 Y" r从2015/11/01开始的新记录来演示这种情况。在这种情况下,月平均保费从120增加到240。屏幕截图也被删除,以使问题更容易读取。
9 ?" q* A# \" d" N7 p0 W+ a: ?DROP TABLE MYTABLE2;CREATE TABLE MYTABLE2 (POLICY_ID NUMBER(11),PAYMENT_DATE DATE,PREMIUM NUMBER(5),YEARLY_PAYMENT_FREQ NUMBER(2),MONTHLY_PREMIUM_DESIRED NUMBER(5));INSERT INTO MYTABLE2 VALUES (1,DATE '2014-10-120、120、120);INSERT INTO MYTABLE2 VALUES (1,DATE  '2014-11-360,4,120);INSERT INTO MYTABLE2 VALUES (1,DATE '2014-12-01、0、4、120);INSERT INTO MYTABLE2 VALUES (1,DATE '2015-01-01、0、4、120);INSERT INTO MYTABLE2 VALUES (1,DATE '2015-02-360,4,120);INSERT INTO MYTABLE2 VALUES (1,DATE '2015-03-01、0、4、120);INSERT INTO MYTABLE2 VALUES (1,DATE '2015-04-01、0、4、120);INSERT INTO MYTABLE2 VALUES (1,DATE '2015-05-720,2,120);INSERT INTO MYTABLE2 VALUES (1,DATE '2015-06-01、0、2、120);INSERT INTO MYTABLE2 VALUES (1,DATE '2015-07-01、0、2、120);INSERT INTO MYTABLE2 VALUES (1,DATE '2015-08-01、0、2、120);INSERT INTO MYTABLE2 VALUES (1,DATE '2015-09-01、0、2、120);INSERT INTO MYTABLE2 VALUES (1,DATE '2015-10-01、0、2、120);INSERT INTO MYTABLE2 VALUES (1,DATE '2015-11-01',240,12,240);INSERT INTO MYTABLE2 VALUES (1,DATE '2016-12-01、240、12、240)--newly added recordsINSERT INTO MYTABLE2 VALUES (1,DATE '2016-01-01、960、4、240);    --newly added recordsINSERT INTO MYTABLE2 VALUES (1,DATE '2016-02-01、0、4、240);   --newly added recordsINSERT INTO MYTABLE2 VALUES (1,DATE '2016-03-01、0、4、240);   --newly added recordsINSERT INTO MYTABLE2 VALUES (1,DATE '2016-04-01、0、4、240);   --newly added recordsINSERT INTO MYTABLE2 VALUES (1,DATE '2016-05-01、960、4、240);    --newly added recordsINSERT INTO MYTABLE2 VALUES (1,DATE '2016-06-01、0、4、240);   --newly added recordsINSERT INTO MYTABLE2 VALUES (1,DATE '2016-07-01、0、4、240);   --newly added recordsINSERT INTO MYTABLE2 VALUES (1,DATE '2016-08-01、0、4、240);   --newly added recordsINSERT INTO MYTABLE2 VALUES (2,DATE '2015-01-60,3,15);INSERT INTO MYTABLE2 VALUES (2,DATE '2015-02-01,0,3,15;INSERT INTO MYTABLE2 VALUES (2,DATE '2015-03-01,0,3,15;INSERT INTO MYTABLE2 VALUES (2,DATE '2015-04-01,0,3,15;INSERT INTO MYTABLE2 VALUES (2,DATE '2015-05-180,1,15);INSERT INTO MYTABLE2 VALUES (2,DATE '2015-06-01、0、1、15);INSERT INTO MYTABLE2 VALUES (2,DATE '2015-07-01、0、1、15);INSERT INTO MYTABLE2 VALUES (2,DATE '2015-08-01、0、1、15);INSERT INTO MYTABLE2 VALUES (2,DATE '2015-09-01、0、1、15);INSERT INTO MYTABLE2 VALUES (2,DATE '2015-10-01、0、1、15);INSERT INTO MYTABLE2 VALUES (2,DATE '2015-11-01、0、1、15);INSERT INTO MYTABLE2 VALUES (2,DATE '2015-12-01、0、1、15);INSERT INTO MYTABLE2 VALUES (2,DATE '2016-01-01、0、1、15);INSERT INTO MYTABLE2 VALUES (2,DATE '2016-02-01、0、1、15);INSERT INTO MYTABLE2 VALUES (2,DATE '2016-03-01、0、1、15);INSERT INTO MYTABLE2 VALUES (2,DATE '2016-04-01、0、1、15);INSERT INTO MYTABLE2 VALUES (2,DATE '2016-05-15、12、15;INSERT INTO MYTABLE2 VALUES (2,DATE '2016-06-15、12、15;SELECT * FROM MYTABLE2.                  * h% J: }4 r0 ~5 a
    解决方案:
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则