samgbelton
samgbelton

Reputation: 89

SQL query that combines 3 tables

In my Access database I have a three different tables based on a pizza company:

enter image description here

enter image description here

enter image description here

What query can I write to show the names of all the delivery staff and the number of items they have delivered? I understand I have to use table aliases but as I am new to SQL I am having problems finding a solution.

Upvotes: 0

Views: 99

Answers (2)

Jithin Shaji
Jithin Shaji

Reputation: 6073

In MS SQL, i think the below code will work.

SELECT  DS.StaffId,COUNT(OI.OrderId) [Count]
FROM    DeliveryStaff DS
JOIN    Orders O ON DS.StaffId = O.DeliveryStaffId
JOIN    OrderItems OI ON O.OrderId = OI.OrderId
GROUP BY DS.StaffId

Upvotes: 5

Try this:

select StaffId, fName, lName, sum(Quantity) 
from DeliveryStaff ds, Orders o, OrderItems oi 
where StaffId = DeliveryStaffId 
  and o.OrderId = oi.OrderId 
group by StaffId, fName, lName

Upvotes: -1

Related Questions