user3408283
user3408283

Reputation: 1

PostgreSQL: summing values that share a field

For a given schema of Olympians in PostgreSQL 9.3.1

Olympics (
    athlete varchar(33), 
    age int, 
    country varchar(21), 
    year int, 
    closing_ceremony_date timestamp, 
    sport varchar(25), 
    gold_medals int, 
    silver_medals int, 
    bronze_medals int, 
    total_medals int
)

It lists different Olympians and the year they competed and the medals they won. However, repeat Olympians may have won medals in separate years, and I need to find out how many total medals each Olympian has won. Thanks!

Upvotes: 0

Views: 23

Answers (1)

iTech
iTech

Reputation: 18450

You will need to do something like the below example

Select athlete, sum(gold_medals) + sum(silver_medals) + sum(bronze_medals) 
       from Olympics group by athlete

Or if the field total_medals is populated correctly you can simply select it directly:

Select athlete, sum(total_medals )
   from Olympics group by athlete

Upvotes: 1

Related Questions