PostgreSQL SUM

PostgreSQL SUM

The PostgreSQL SUM aggregate function returns the sum value of all rows in a select.

SUM syntax

SELECT SUM(table_column)
FROM table_name;

SUM example

Goods table

idgood_typenamedescriptionprice
1ACar_1Car 1 description100
2ACar_2Car 2 description200
3ACar_3Car 3 description100
4BBoat_4Boat 4 description500
5BBoat_5Boat 5 description300
6CTrain_1Train 123 description800
SELECT SUM(price) 
FROM goods;

Result

2000

SELECT good_type, SUM(price) sum_price
FROM goods
GROUP BY good_type;

Result

good_typesum_price
B800
C800
A400