如何找到特定的员工总金额

时间:2018-10-10 08:50:45

标签: sql sql-server database

我有一个员工表。该表具有三列:CustomerNameRoleSalary。在CustomerName列中,User1出现了很多次,但我只希望其中一行与User1的总薪水相符。下面,我粘贴了一些示例数据以供参考。

+--------------+------------+--------+
| CustomerName |    Role    | Salary |
+--------------+------------+--------+
| User1        | Design     |    100 |
| User2        | Developer  |    100 |
| User1        | Design     |    100 |
| User3        | Programmer |    100 |
| User1        | Design     |    100 |
+--------------+------------+--------+

输出应类似于:

+--------------+------------+----------+
| CustomerName |     Role   |  Salary  |
+--------------+------------+----------+
| user1        | Design     |      300 |
| user2        | Developer  |      100 |
| user3        | Programmer |      100 |
+--------------+------------+----------+

我想要User1的工资总额。如何做到这一点?

2 个答案:

答案 0 :(得分:5)

使用分组依据进行聚合

select customername,role, sum(salary) as salary
from tablename
group by customername, role

答案 1 :(得分:2)

对于user1

select customername,role,sum(salary) from yourtable where customername='user1' group by customername,role