Join between two master and one detail Table - sql

I am working with Northwind Data Base(Microsoft Example) and i want to know total Sale of each Region.
tables for this example are (Region,Territories,EmployeeTerritories,Employees,Orders and OrderDetails)
And relations are:
Region on to many Territories
Territories on to many EmployeeTerritories
EmployeeTerritories many to one Employees
Employees on to many Orders
Orders one to many OrderDetails
i know that i should to use Sub query but i dont know how.
select
*
from
Region R
inner join Territories T
on R.RegionID=T.RegionID
inner join EmployeeTerritories ET
on T.TerritoryID=ET.TerritoryID
inner join
(
select
E.EmployeeID,
E.FirstName,
E.LastName,
sum(OD.Quantity*OD.UnitPrice) as TotalEmployeeSale
from
Employees E
inner join Orders O
on E.EmployeeID=o.EmployeeID
inner join [Order Details] OD
on o.OrderID=OD.OrderID
Group by
E.EmployeeID,
E.FirstName,
E.LastName
)as ES on ET.EmployeeID=ES.EmployeeID
I use this sql query to calculate Total sale for Each Employee but how can i Caclulate is for Each Region.

Haven't got access to Northwind right now, so this is untested, but you should get the idea...
There's no need to get data about employees if all you want is sales per region. Your subquery is therefore redundant...
select
r.RegionDescription,
sum(OD.Quantity*OD.UnitPrice)
from
Region R
inner join Territories T
on R.RegionID=T.RegionID
inner join EmployeeTerritories ET
on T.TerritoryID=ET.TerritoryID
inner join Employees E
on ET.EmployeeID=E.EmployeeID
inner join Orders O
on E.EmployeeID=o.EmployeeID
inner join [Order Details] OD
on o.OrderID=OD.OrderID
Group by r.RegionDescription
As discussed in the comments, this "double counts" sales where an employee is assigned to more than one region. In many cases, this is desired behaviour - if you want to know how well a region is doing, you need to know how many sales came from that region, and if an employee is assigned to more than one region, that doesn't affect the region's performance.
However, it means you overstate the sales if you add up all the regions.
There are two strategies to avoid this. One is to assign the sale to just one region; in the comments, you say there's no data on which to make that decision, so you could do it on the "lowest regionID) - something like:
select
r.RegionDescription,
sum(OD.Quantity*OD.UnitPrice)
from
Region R
inner join Territories T
on R.RegionID=T.RegionID
inner join EmployeeTerritories ET
on T.TerritoryID=ET.TerritoryID
inner join Employees E
on ET.EmployeeID=E.EmployeeID
inner join Orders O
on E.EmployeeID=o.EmployeeID
inner join [Order Details] OD
on o.OrderID=OD.OrderID
Group by r.RegionDescription
having et.TerritoryID = min(territoryID)
(again, no access to DB, so can't test - but this should filter out duplicates).
Alternatively, you can assign a proportion of the sale to each region - though then rounding may cause the totals not to add up properly. That's a query I'd like to try before posting though!

Related

How to get the Total Amount Each Customer Has Spent

I am making a database for a simple purchase, customer, employee, etc tables.
I am making a query that includes all of the employees who have boughten something, along with some of their data, and then the last field is supposed to have the total amount that they have boughten. However, I am not too sure on how to achieve this.
I am not quite sure on how to get the total to be almost like a sub-query to total up what the customer has gotten. For instance, the first 5 rows should have a total of 180.40.
I have been looking online for some help with totaling queries but couldn't find any good examples.
Any help would be wonderful! Thank you (I am pretty new to SQL and Access)!
Edit: Forgot to add this!
SELECT Employee.FirstName, Employee.LastName, Purchase.PurchaseID, Product.ProductName, Product.Price, Product.Price AS Total
FROM Employee
INNER JOIN (Customer INNER JOIN (Product INNER JOIN Purchase ON Product.ProductID = Purchase.ProductID)
ON Customer.CustomerID = Purchase.CustomerID) ON Employee.EmployeeID = Customer.EmployeeID;
Do you just want aggregation?
SELECT e.FirstName, e.LastName,
SUM(p.Price)
FROM ((Employee as e INNER JOIN
Customer as c
ON e.EmployeeID = c.EmployeeID
) INNER JOIN
Purchase pu
ON c.CustomerID = pu.CustomerID
) INNER JOIN
Product as p
ON p.ProductID = pu.ProductID
GROUP BY e.FirstName, e.LastName;

SQL Comparison query

I am doing practice queries to improve my SQL skills (I'm a beginner) and I ran into this problem that I need help with using the Northwind database. The requested query should:
Give the name of employees and the city where they live for employees who have
sold to customers in the same city.
What I wrote for this was:
USE Northwind;
SELECT DISTINCT FirstName, LastName, e.City
FROM Employees e
INNER JOIN Orders o ON e.EmployeeID = O.EmployeeID
INNER JOIN Customers c ON o.CustomerID = c.CustomerID
INNER JOIN Customers ON c.City = e.City
WHERE e.City = ANY(SELECT Customers.City FROM Customers);
I am returned 6 employees but I am not sure that they are correct and I believe that my WHERE statement is the wrong one as well.
Basically my question is, am I using the correct join statements and how do I go about filtering results with the WHERE statement? I'm not exactly sure how to compare one specific record with other records. Coming from a Java background I am used to for-loops that can check each individual "object" (record) with a specific field from another "object". In this case I am wondering how I can check the City attribute of each record from the Employees table with the City attribute of the records on the Customers table. Any and all advice is appreciated, thanks!
I think you only need to join the customer table once and have both requirements (being on the same order and same city as the employee) as your join requisites e.g
SELECT DISTINCT FirstName, LastName, e.City
FROM Employees e
INNER JOIN Orders o ON e.EmployeeID = O.EmployeeID
INNER JOIN Customers c ON o.CustomerID = c.CustomerID AND c.City = e.City
Alternatively you can just join the customer on the order id and filter the city requirement in the where clause. Performance-wise there shouldn't be any difference however if you are going to look back on the script at some point it may help you to remember
SELECT DISTINCT FirstName, LastName, e.City
FROM Employees e
INNER JOIN Orders o ON e.EmployeeID = O.EmployeeID
INNER JOIN Customers c ON o.CustomerID = c.CustomerID
WHERE e.City = C.City
I think the important part to get your head round is that after you have joined the table it is effectively one table with columns from both (or more with multiple joins). The inner join conditions will filter out rows that do not have a match between the two tables and then you are left to compare columns

Select IDs which do not JOIN

I want to get the list of ProductId which has not been ordered by an Employee.
I have this query which gives me all ProductId which has been ordered, but I want reverse of it:
SELECT distinct e.EmployeeID ,p.ProductID
FROM Products p JOIN OrderDetails od
ON od.ProductID=p.ProductID JOIN Orders o
ON o.OrderID=od.OrderID JOIN Employee e
ON e.EmployeeID=o.EmployeeID
order by e.EmployeeID
I'm not sure to understand your question, but if you want products without Employee associated you can try this :
SELECT distinct e.EmployeeID ,p.ProductID
FROM Products p
INNER JOIN OrderDetails od
ON od.ProductID=p.ProductID
INNER JOIN Orders o
ON o.OrderID=od.OrderID
LEFT JOIN Employee e
ON e.EmployeeID=o.EmployeeID
WHERE e.EmployeeID is NULL
order by e.EmployeeID
The idea is to select all the orders (INNER JOIN) also orders who haven't Employee associated (LEFT JOIN). Finally you filter on those which haven't association (IS NULL).

SQL-Server, confront Customers with Employees function

I might have a really simple question, but it doesn't seem i can find the solution yet. Basically, i have to write a SQL-Server function, based on Northwind DB. It has to:
Take 2 dates as arguments and display, without repetition these Customer's data ID | Name | City | Address , for those customers where the total purchase he had made from at least one Employee , is greater than the average sale made by this Employee between the two dates.
So the main steps should be:
1. Retrieve the total purchase made from a Customer from each Employee. I know how to get the total purchase from each Company:
SELECT
Customers.CompanyName, SUM(UnitPrice*Quantity)
FROM Orders inner join [Order Details]
ON Orders.OrderID=[Order Details].OrderID INNER JOIN
Customers ON Orders.CustomerID=Customers.CustomerID
GROUP BY Customers.CompanyName
But how can i get those made from each employee?
2.Confront this with the average sales of this Employee between the given dates. I can get the average for each employee:
SELECT FirstName+' '+LastName, AVG(UnitPrice*Quantity)
FROM Orders inner join [Order Details]
ON Orders.OrderID=[Order Details].OrderID
INNER JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID
WHERE OrderDate BETWEEN #dt1 and #dt2
GROUP BY FirstName+' '+LastName
Note that i'm only pasting the query part, but here, the Employee should depend on the first query (probably this should be put inside a subquery)
Everything should be put inside a single function (it should not be split in two). The Northwind DB diagram is: Northwind Diagram . Please help!
Hope I got the logic right:
create function x (#from datetime, #to datetime)
returns table
as
return (
with cust as (
select o.customerid, o.employeeid, sum(unitprice*quantity) as cust_purchase
from orders o
inner join [order details] d on o.orderid=d.orderid
where o.orderdate between #from and #to
group by o.customerid, o.employeeid
),
emp as (
select o.employeeid, avg(unitprice*quantity) as emp_sale
from orders o
inner join [order details] d on o.orderid=d.orderid
where o.orderdate between #from and #to
group by o.employeeid
)
select c.customerid, c.companyname, c.city, c.address
from cust
inner join emp on cust.employeeid = emp.employeeid
and cust.cust_purchase > emp.emp_sale
inner join customers c on cust.customerid = c.customerid
)
go
select * from x ('19980401', '19980430')

TSQL basic join on Northwind database

I am learning TSQL (well, just SQL to tell the truth) and I want to make Employee - Product statistic on Northwind database.
Expected results should be something like:
EmployeeID | ProductID | income
1 | 1 | 990
1 | 2 | 190
1 | 3 | 0
...
For all Employy-Product pairs
My first try is this query:
SELECT E.EmployeeID, OE.ProductID, SUM(OE.ExtendedPrice) as income
FROM [Order Details Extended] OE
JOIN [Orders] O
ON OE.OrderID = O.OrderID
RIGHT OUTER JOIN Employees E
ON E.EmployeeID = O.EmployeeID
GROUP BY E.EmployeeID, OE.ProductID
ORDER BY E.EmployeeID
But I don't get results for all pairs.
What am I doing wrong?
HLGEM missed few columns but I understood what he tried to do.
I came up with this:
SELECT A.employeeid, A.productid, SUM(Oe.ExtendedPrice) AS income
FROM
(SELECT E.Employeeid, P.productid
FROM employees E
CROSS JOIN products P) A
LEFT JOIN [Order Details Extended] OE
ON A.productid = OE.productid
LEFT JOIN [Orders] O
ON OE.OrderID = O.OrderID
GROUP BY A.EmployeeID, A.ProductID
ORDER BY A.EmployeeID, A.ProductID
This returns results for all pairs, but those don't seem right.
For example above query returns as first row:
1, 1, 12788.10
But this query:
SELECT SUM(ODE.ExtendedPrice) FROM [Order Details Extended] ODE
LEFT JOIN [Orders] OD
ON ODE.OrderID = OD.OrderID
WHERE OD.EmployeeID = 1 AND ODE.ProductID = 1
Returns 990.90.
Why?
edit
I got it finally:
SELECT A.EmployeeId, A.ProductId, ISNULL(SUM(Oe.ExtendedPrice), 0) AS income
FROM
(SELECT E.Employeeid, P.productid
FROM [Employees] E
CROSS JOIN [Products] P) A
LEFT JOIN [Orders] O
ON O.EmployeeID = A.EmployeeID
LEFT JOIN [Order Details Extended] OE
ON A.productid = OE.productid AND OE.OrderID = O.OrderID
GROUP BY A.EmployeeID, A.ProductID
ORDER BY A.EmployeeID, A.ProductID
#HLGEM you can copy/paste this solution to your answer so I can accept it.
You could try:
SELECT A.employeeid,A.product_id, SUM(Oe.ExtendedPrice) AS income
FROM
(SELECT E.Employeeid, P.product id
FROM employee E
CROSS JOIN product p) A
LEFT JOIN [Order Details Extended] OE
ON A.EmployeeID = O.EmployeeID
LEFT JOIN [Orders] O
ON OE.OrderID = O.OrderID
GROUP BY A.EmployeeID, OE.ProductID
ORDER BY A.EmployeeID
I switched it to a LEFT JOIN as most people use them instead of right joins and thus they are easier for maintenance.
What results are you missing? It looks like you have used an inner join between Order Details Extended and Orders, so any orders that don't have details will be excluded. It seems logical that you would want this, since you are summing a value in the details table.
Then you do a right outer join with Employees, so you are including all employees, regardless of whether they have any orders. This also makes sense, since it looks as though you are seeing which employees sold which products.
Your query will only give you the data for products that the employees have actually sold. If you are looking for all employee-product combinations, regardless of whether the employee had ever sold that product, you will want to join the Product table as well, although in my muddled morning, I can't think if you would want an outer join or a cross join.
In all, I think your first try is a good one. Pretty complex for a beginner. Nice effort!