razorcode7
razorcode7

Reputation: 55

Joining and aggregating data from multiple tables

In customer table there is a SupportID which is the WorkerId in the worker Table, each WorkerId shows which will handle that customer.

Working Name | No. of accounts | total revenue 
----------------------------------------------
John McCoy     20                10,000

At the moment I have this code / idea

SELECT FirstName, LastName
FROM Employee A, Invoice B
WHERE A.EmployeeId = B.CustomerId

Upvotes: 0

Views: 119

Answers (2)

In this question, you have not mention dependency among worker and receipt table. However, let the dependency column is workerId on table receipt. Now try this, hope you will get your desired result.

select a.firstName, sum(count(b.customerId)) as no_accounts, sum(c.unitPrice *c.quantity) as total_revenue 
from (( worker a join receipt b on a.workerId = b.SupportId) 
join receiptLine c on b.receiptId = c.receiptId) group by a.firstName order by a.firstName;

Upvotes: 1

Quốc Hồ Thanh
Quốc Hồ Thanh

Reputation: 35

use GROUP BY to resolve your problem

Upvotes: 0

Related Questions