How to aggregate the rows of a table to create a new one in sql

Viewed 16

I am working with sql and I would like to do the following. I have a table

Name Limit Use
Person1 100 50
Person1 200 200
Person2 200 0
Person3 100 100
Person3 200 50
Person3 300 50

And I want as a result

Name Limit Use
Person1 300 250
Person2 200 0
Person3 600 200

This means I want to create a table that has only one row for each distinct name and aggregates the rows for each different name by summing them. Would be happy for any help!

1 Answers

What you want is "aggregation". Use a group by clause and then add the values together with the aggregate function sum.

select
  name,
  sum(limit) as limit,
  sum(use) as use
from that_table
group by name
Related