开发者

SQL find name doubles and sum values

I have one table records with the columns:

|rec.id|rec.name|user.name|hours|

and the values respectively:

|1     |google  |Admin    | 12  |
|2     |yahoo   |Admin    | 1   |
|3     |bing    |Manager  | 4   |

What i want to do is take all of the records with the same user.id and sum there hours together in SQL. Perhaps its the early mornign but i cant seem to figure out a way of doing this. I thought about开发者_Go百科 using sql to find the duplicates but thats only going to return a number and not what i want to do with them. This sounds like a really simple thing so sorry in advance.


select   user_name,
         sum(hours)
from     your_table
group by user_name;


You would group on the user name and use the sum aggregate on the hours:

select [user.name], sum(hours) as hours
from TheTable
group by [user.name]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜