如何在SQL中将两个字段组合在一起?

时间:2016-05-07 23:05:19

标签: sql database oracle

说我有一个sql,目前返回所有在每年玩过的足球运动员。像这样:

  name       year      goals
john        2010        1 
john        2006        2
john        2006        8
fred        2006        1

但是我希望结果按照他们玩的年份进行分组,但如果他们来自不同年份,请不要压缩玩家名称,如下所示:

 name     year      goals
john      2010       1
john      2006       10        <--- This is compressed, but there are still 2 johns
fred      2006       1              since they are from different years

说我到目前为止已经这样做了。

(select name, year, goals
from table) as T 

如果我这样做

select *
from
  (select name, year, goals
  from table) as T 
group by year;
弗雷德将会消失,但如果我按“名字分组”,只剩下一个约翰。有帮助吗?

1 个答案:

答案 0 :(得分:6)

select name, year, sum(goals) as totalgoals
from table
group by name, year