PostgreSQL:汇总共享字段的值

时间:2014-03-11 22:53:58

标签: database postgresql sum

对于PostgreSQL 9.3.1中给定的Olympians模式

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
)

它列出了不同的奥运选手和他们参加比赛的那一年以及他们赢得的奖牌。然而,重复的奥运选手可能在不同的年份赢得了奖牌,我需要找出每个奥运选手赢得的总奖牌数。谢谢!

1 个答案:

答案 0 :(得分:1)

您需要执行类似以下示例的操作

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

如果正确填充了字段total_medals,您只需直接选择它:

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