给定表employees,我需要编写一个查询来获取每个部门的总工资。表中任何重复的e\u id都不能计数两次(在本例中是molly)。
表如下所示:
(请原谅我插入数据表的方式,我不太习惯在上面发问)
<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条答案
按热度按时间kx1ctssn1#
你可以试着用
distinct
在子查询中按行删除重复项dept_id
那就做吧SUM
查询#1db fiddle视图