SQL Quick Tip: Calculate Cumulative Sums with Ease! #sql #interview #sqltips #database

preview_player
Показать описание
For each row in the employees table, the SUM(salary) OVER (ORDER BY id) function computes the sum of salary from the beginning of the dataset up to the current row, based on the ordering by id.
This creates a cumulative or running total of the salary column.

Explanation of Output:

Row 1: The cumulative sum is just the salary of id 1, which is 30,000.
Row 2: The cumulative sum is 30,000 + 45,000 = 75,000.
Row 3: The cumulative sum is 75,000 + 50,000 = 125,000.
Row 4: The cumulative sum is 125,000 + 55,000 = 180,000.
Row 5: The cumulative sum is 180,000 + 40,000 = 220,000.
Рекомендации по теме