在Oracle窗口函数中求和

时间:2013-01-03 12:20:21

标签: sql oracle window-functions

任何人都可以在oracle分区的sum sum内告诉我(SUM(SUM(VAL)))的重要性,请参阅以下代码段

SELECT NMS,
       SUM(SUM(VAL)) OVER(PARTITION BY NMS),  ---- SIGNIFICANCE
       EMPNO,
       SUM(SUM(VAL)) OVER(PARTITION BY EMPNO)   ---- SIGNIFICANCE
  FROM PART_TEST_PART
 GROUP BY NMS, EMPNO;

1 个答案:

答案 0 :(得分:6)

计算VALNMS(第一个)和EMPNO(第二个)的总和。

首先评估组+ SUM(VAL),然后分析计算该结果。

e.g。如果您在

组之后得到了结果
EMPNO  NMS SUM(VAL)
1      a   15
1      b   24
2      a   50

你有SUM(SUM(VAL)) OVER(PARTITION BY NMS)

EMPNO  NMS SUM(VAL) SUM(SUM(VAL)) OVER(PARTITION BY NMS)
1      a   15       65  (15+50 for NMS = a)
1      b   24       24  (24 for NMS = b)
2      a   50       65  (15+50 for NMS = a)

你有SUM(SUM(VAL)) OVER(PARTITION BY EMPNO)

EMPNO  NMS SUM(VAL) SUM(SUM(VAL)) OVER(PARTITION BY EMPNO)
1      a   15       39  (15+24 for EMPNO = 1)
1      b   24       39  (15+24 for EMPNO = 1)
2      a   50       50  (50 for EMPNO = 2)