WINDOW FUNCTION SUM

description

Calculate the sum of the data in the window

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

example

Group by property, and calculate the sum of the x columns of the current row and the previous row within the group.

  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