使用MySQL

时间:2018-09-30 21:48:44

标签: mysql sql duplicates distinct

鉴于表中的员工,我需要编写查询以获取每个部门的总薪水。表中任何重复的e_id均不得计数两次(在这种情况下为Molly)。

该表如下所示:

(请原谅我插入数据表的方式,我是刚开始在SO上发布问题的人)

<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg .tg-0pky{border-color:inherit;text-align:left;vertical-align:top}
.tg .tg-0lax{text-align:left;vertical-align:top}
</style>
<table class="tg">
  <tr>
    <th class="tg-0pky">e_id</th>
    <th class="tg-0lax">l_name</th>
    <th class="tg-0lax">f_name</th>
    <th class="tg-0lax">salary</th>
    <th class="tg-0lax">dept_id</th>
  </tr>
  <tr>
    <td class="tg-0lax">12</td>
    <td class="tg-0lax">grace</td>
    <td class="tg-0lax">Barbara</td>
    <td class="tg-0lax">8400</td>
    <td class="tg-0lax">1</td>
  </tr>
  <tr>
    <td class="tg-0lax">34</td>
    <td class="tg-0lax">Gates</td>
    <td class="tg-0lax">David</td>
    <td class="tg-0lax">8000</td>
    <td class="tg-0lax">1</td>
  </tr>
  <tr>
    <td class="tg-0lax">39</td>
    <td class="tg-0lax">Patrick</td>
    <td class="tg-0lax">Neil</td>
    <td class="tg-0lax">7500</td>
    <td class="tg-0lax">1</td>
  </tr>
  <tr>
    <td class="tg-0lax">45</td>
    <td class="tg-0lax">Park</td>
    <td class="tg-0lax">Molly</td>
    <td class="tg-0lax">8100</td>
    <td class="tg-0lax">2</td>
  </tr>
  <tr>
    <td class="tg-0lax">45</td>
    <td class="tg-0lax">Park</td>
    <td class="tg-0lax">Molly</td>
    <td class="tg-0lax">8100</td>
    <td class="tg-0lax">2</td>
  </tr>
  <tr>
    <td class="tg-0lax">75</td>
    <td class="tg-0lax">Gates</td>
    <td class="tg-0lax">Ryan</td>
    <td class="tg-0lax">8300</td>
    <td class="tg-0lax">2</td>
  </tr>
</table>

所需的输出是:

<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg .tg-s268{text-align:left}
.tg .tg-0lax{text-align:left;vertical-align:top}
</style>
<table class="tg">
  <tr>
    <th class="tg-s268">Uniq_Dept</th>
    <th class="tg-s268">TotalSalary</th>
  </tr>
  <tr>
    <td class="tg-s268">1</td>
    <td class="tg-s268">23900</td>
  </tr>
  <tr>
    <td class="tg-0lax">2</td>
    <td class="tg-0lax">16400</td>
  </tr>
</table>

1 个答案:

答案 0 :(得分:3)

您可以尝试在子查询中使用distinct来删除dept_id的重复行,然后执行SUM

查询#1

SELECT dept_id as Uniq_Dept,SUM(salary) as TotalSalary 
FROM (
  SELECT distinct dept_id, salary
  FROM t 
) t1
GROUP BY dept_id;

| Uniq_Dept | TotalSalary |
| --------- | ----------- |
| 1         | 23900       |
| 2         | 16400       |

View on DB Fiddle