选择表 1 中的列并使用它来选择表 2 中的总和的 SQL 查询 [英] SQL Query that selects a column in table 1 and uses that to select sum in table 2
本文介绍了选择表 1 中的列并使用它来选择表 2 中的总和的 SQL 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 2 个表 1,其中包含员工和群组列表,而 1 个包含每个员工的销售额,表 2 没有群组编号,但包含员工姓名列表.我需要按组号选择并按名称显示名称结果和总销售额.表一:
I have 2 tables 1 with a list of employees and the group and 1 with the sales of each employee, Table 2 does not have the group number but has a list of employee names. i need to select by group number and display Name results and the total sales by name. Table 1:
Name|Group
*John|1*
*Mary|2*
*Will|1*
*Jane|2*
表 2
Name| Sale
*Will|20*
*John|10*
*Mary|10*
*Jane|20*
*John|20*
*Will|20*
*Mary|20*
我需要结果来选择第 1 组并按名称汇总结果,结果应该如下所示.结果表
I need the result to select group 1 and sum results by name, result should look like. Result Table
Name|Sum(Sale)
*John|30*
*Will|40*
推荐答案
这是一个非常基本的JOIN
和GROUP BY
:
This is a pretty basic JOIN
and GROUP BY
:
select t2.name, sum(t2.sale)
from table2 t2 join
table1 t1
on t2.name = t1.name
where t1.group = 1
group by t2.name
这篇关于选择表 1 中的列并使用它来选择表 2 中的总和的 SQL 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文