到目前为止,我有一份员工名单,其中有客户和他们有多少人。
SELECT Employee.FirstName || ' ' || Employee.LastName AS 'Employee Name'
, COUNT(SupportRepID) AS 'No. Of Accounts'
FROM Employee
Join Customer
On Customer.SupportRepID = Employee.EmployeeID
Group By EmployeeID
我想要的最终产出是这个,并且每个员工的发票总收入也是如此。这意味着我需要计算每个客户的总发票数,然后计算连接到员工的所有客户的总发票。我在下面计算了每个客户的发票:
SELECT InvoiceID
FROM Invoice
GROUP BY CustomerID
我有一个模糊的想法,但我不知道如何很好地加入表格,我们将不胜感激。
答案 0 :(得分:0)
我在这里做了一些猜测,因为我没有看到你的“发票价值”查询是如何工作的,所以我认为它是缺少的。基本上我认为你想将你的发票价值查询添加到原始查询,以合并结果?
如果是这种情况,那么您可以使用子查询,如下所示:
SELECT
e.Employee.FirstName || ' ' || e.Employee.LastName AS 'Employee Name',
COUNT(c.SupportRepID) AS 'No. Of Accounts',
SUM(i.InvoiceValue) AS 'Value Of Invoices'
FROM
Employee e
INNER JOIN Customer c ON c.SupportRepID = e.EmployeeID
LEFT JOIN (
SELECT
CustomerID,
SUM(InvoiceValue) AS InvoiceValue
FROM
Invoice
GROUP BY
CustomerID) i ON i.CustomerId = c.CustomerId
GROUP BY
e.EmployeeID
请注意,我更改了“发票金额”查询的逻辑,我不得不猜测哪个列可能包含该值。