我正在编写一个查询,其中列出了银行场景中特定员工服务的客户数量。
桌子的设计如下:
Table Design
以下查询产生了我需要的结果:
SELECT
EMPID, COUNT(EMPID) AS 'Customers Served'
FROM
Financial.AccountOperations
GROUP BY
EMPID;
SELECT
EMPID, COUNT(EMPID) AS 'Customers Served'
FROM
Financial.LoanOperations
GROUP BY
EMPID;
Query Results
我正试着把这些问题合并成一个问题。我尝试过使用
UNION
、INTERSECT
、JOIN
和子查询,但没有成功。有人能帮忙吗?
所需输出如下:
EMPID Customers Served
2 2
3 3
4 17
5 16
7 2
8 4
9 8
10 5
12 1
15 1
最佳答案
我想@hogan有了它-再往前走一步:
SELECT EMPID, Sum([Customers Served]) AS NumServed FROM
(SELECT EMPID,COUNT(EMPID) AS 'Customers Served'
FROM Financial.AccountOperations
GROUP BY EMPID
UNION
SELECT EMPID,COUNT(EMPID) AS 'Customers Served'
FROM Financial.LoanOperations
GROUP BY EMPID)
GROUP BY EMPID
关于sql-server - SQL Server:具有联接的多个计数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38017964/