WINDOW FUNCTION SUM

description

计算窗口内数据的和

  1. SUM([DISTINCT | ALL] expression) [OVER (analytic_clause)]

example

按照 property 进行分组,在组内计算当前行以及前后各一行的x列的和。

  1. select x, property,
  2. sum(x) over
  3. (
  4. partition by property
  5. order by x
  6. rows between 1 preceding and 1 following
  7. ) as 'moving total'
  8. from int_t where property in ('odd','even');
  9. | x | property | moving total |
  10. |----|----------|--------------|
  11. | 2 | even | 6 |
  12. | 4 | even | 12 |
  13. | 6 | even | 18 |
  14. | 8 | even | 24 |
  15. | 10 | even | 18 |
  16. | 1 | odd | 4 |
  17. | 3 | odd | 9 |
  18. | 5 | odd | 15 |
  19. | 7 | odd | 21 |
  20. | 9 | odd | 16 |

keywords

  1. WINDOW,FUNCTION,SUM