PostgreSQL: How to Calculate Sum by Group


Often in PostgreSQL you may want to calculate the sum of one column, grouped by another column.

You can use the following syntax to do so:

SELECT team,
       SUM(points) AS sum_points
FROM athletes
GROUP BY team;

This particular example will calculate the sum of the points column, grouped by the team column in the table named athletes.

The following example shows how to use this syntax in practice.

Example: How to Calculate Sum by Group in PostgreSQL

Suppose that we create the following table named athletes that contains information about points scored by various basketball players:

-- create table named 'athletes'
CREATE TABLE athletes (
  id INTEGER PRIMARY KEY,
  team TEXT,
  points INTEGER
);

-- add rows to the table 
INSERT INTO athletes VALUES (0001, 'Mavs', 22);
INSERT INTO athletes VALUES (0002, 'Mavs', 30);
INSERT INTO athletes VALUES (0003, 'Mavs', 12);
INSERT INTO athletes VALUES (0004, 'Mavs', 9);
INSERT INTO athletes VALUES (0005, 'Mavs', 14);
INSERT INTO athletes VALUES (0006, 'Heat', 16);
INSERT INTO athletes VALUES (0007, 'Heat', 40);
INSERT INTO athletes VALUES (0008, 'Heat', 18);
INSERT INTO athletes VALUES (0009, 'Heat', 16);
INSERT INTO athletes VALUES (0010, 'Heat', 33);

-- view all rows in table
SELECT * FROM athletes;

 id | team | points 
----+------+--------
  1 | Mavs |     22
  2 | Mavs |     30
  3 | Mavs |     12
  4 | Mavs |      9
  5 | Mavs |     14
  6 | Heat |     16
  7 | Heat |     40
  8 | Heat |     18
  9 | Heat |     16
 10 | Heat |     33

Suppose that we would like to calculate the sum of points scored by players on each team.

We can use the following syntax to do so:

SELECT team,
       SUM(points) AS sum_points
FROM athletes
GROUP BY team;

 team | sum_points 
------+------------
 Heat |        123
 Mavs |         87

From the resulting table we can see:

  • The sum of points scored by players on the Heat is 123.
  • The sum of points scored by players on the Mavs is 87.

We can verify that these values are correct by manually calculating the sum of the values in the points column for each team:

  • Heat: 16 + 40 + 18 + 16 + 33 = 123.
  • Mavs: 22 + 30 + 12 + 9 + 14 = 87.

These values match the values in the output table.

Feel free to use similar syntax to calculate a sum by group in your own table.

Additional Resources

The following tutorials explain how to perform other common tasks in PostgreSQL:

PostgreSQL: How to Select Top N Percent of Records
PostgreSQL: How to Calculate a Moving Average
PostgreSQL: How to Calculate Average Across Multiple Columns

Leave a Reply