使用distinct和sum查找总薪水?

时间:2014-04-04 08:58:44

标签: mysql

如何获得不同身份证的总和及其各自的薪水?

ID   Salary
1    1000
2    2000
1    1000
2    2000

在上面我希望得到像这样的

Total salary 
3000

我尝试了这样的输出

ID   Salary
1    1000
2    2000

select distinct(id), sum (salary) from employee group by id

主要问题是,在对ID应用不同之后我无法完成。

想要删除id的重复条目和那些不同ID的总和吗?

5 个答案:

答案 0 :(得分:1)

检查此查询是否

查询

SELECT SUM (
    DISTINCT salary
    ) FROM employee

结果

SUM(DISTINCT SALARY)
--------------------
3000

检查 Demo SQLFiddle


你也可以这样做,

SELECT SUM(salary) 
FROM (SELECT DISTINCT id, salary 
   FROM employee 
   GROUP BY id,salary
) AS emp

检查 Demo SQLfiddle

答案 1 :(得分:0)

最简单的方法是删除重复记录并编写简单查询。第二种方式是这个查询,其中我在子查询中为每个雇员取了MAX(Highest)个工资,然后从计算总工资中应用SUM()

    SELECT SUM (sal) from
   (
        SELECT MAX (salary)  as sal
        FROM employee GROUP BY id
    ) as tbl

答案 2 :(得分:0)

尝试这样

select sum(salary) from( select distinct id, salary from 
employee) as t

答案 3 :(得分:0)

尝试这100%的工作

SELECT SUM(salary) as "Total salary" 
        FROM(SELECT DISTINCT id, salary FROM 
           employee GROUP BY id,salary ) as e

答案 4 :(得分:0)

尝试一下: 它肯定会工作 仅具有相同ID的薪水总和。

Select SUM(salary) as total from employee group by id;