I have a sample db with 3 different tables, customers, orders, orderdetails.
The assignment is to show customer name, and address from a customers table and then show each order total amount by order id. Order details has order id several times and it is by unit x price so I have to sum these after performing the calculation.
Customers has a field customerid, which I can use to join with orders which has the same field, the orders table has orderid which I can use to join to orderdetails and sum the order total but I do not know how to put the information together. Customers table does not have the fields to calculate the total order, and only has customerid. So, I'm trying to pinch together from 3 tables where there is some related column but not all present in each table.
I can do 2 separate select statements and each do what I expect but I have been trying to get the info together and have been unable to.
SELECT c.CustomerID, c.[Address], o.orderid
FROM Customers c
Join Orders o
ON c.CustomerID = o.CustomerID
--how to join these together?
SELECT od.orderid, SUM(od.UnitPrice*od.Quantity) as 'Subtotal'
FROM OrderDetails od
Join Orders o
ON od.OrderID = o.OrderID
Group by od.OrderID
I am trying to show this with the following information:
Customer Name, Address, OrderID, and Order Total.
try this -
SELECT c.CustomerID, c.[Address], od.orderid, SUM(od.UnitPrice* od.Quantity) as 'Subtotal'
FROM OrderDetails od
Join Orders o
ON od.OrderID = o.OrderID
join Customers c ON c.CustomerID = o.CustomerID
Group by c.CustomerID, c.[Address], od.OrderID
You can join three tables together like given below. I am using derived table OrderDetails to calculate subtotal at orderId level.
SELECT c.CustomerID, c.[Address], o.orderid, SUM(od.Subtotal) as 'Subtotal'
FROM Customers c
Join Orders o
ON c.CustomerID = o.CustomerID
join (SELECT orderid, SUM(od.UnitPrice*od.Quantity) as Subtotal from OrderDetails od GROUP BY OrderId) as OrderDetails od
ON od.OrderID = o.OrderID
group by c.CustomerID, c.[Address], o.orderid
Mukesh's answer led me directly to the finish, I was able to leave the customerid out of the result with the following. This helped a lot and I appreciate everyone's input.
SELECT c.CompanyName, c.[Address], od.orderid, SUM(od.UnitPrice* od.Quantity) as
'Subtotal'
FROM OrderDetails od
Join Orders o
ON od.OrderID = o.OrderID
join Customers c ON c.CustomerID = o.CustomerID
Group by c.CompanyName, c.[Address], od.OrderID
Related
I am using the Northwind database
For now i have tried
It is where i select the Orders of the Client
select od.ProductID from Customers c JOIN
Orders o on c.CustomerID=o.CustomerID
JOIN [Order Details] od on o.OrderID=od.OrderID
where c.CustomerID='CENTC'
And here is my solution
select distinct c.CompanyName, sum(od.ProductID) as suma from Customers c JOIN
Orders o on c.CustomerID=o.CustomerID
JOIN [Order Details] od on o.OrderID=od.OrderID
where od.ProductID = '40' or od.ProductID = '11'
group by c.CompanyName
having sum(od.ProductID)='51'
But it's a one use solution so i am not satisfied.
You can use an IN subquery for this
SELECT
c.CompanyName,
c.ContactName,
SUM(od.quantity) AS quantity
FROM Customers c
JOIN Orders o on c.CustomerID = o.CustomerID
JOIN OrderDetails od on o.OrderID = od.OrderID
WHERE od.ProductID IN (
SELECT od2.ProductID
FROM Orders o2
JOIN OrderDetails od2 on o2.OrderID = od2.OrderID
WHERE o2.CustomerID = 'CENTC'
)
GROUP BY
c.CustomerID,
c.CompanyName,
c.ContactName;
I'm trying to rank a query by not just one count, but by two.
I want to rank customers by the order items per orders.
WITH CTE AS
(
SELECT
o.CustomerId,
COUNT(DISTINCT o.OrderId) AS OrderCount,
COUNT(oi.OrderItemId) AS OrderItemCount
FROM
OrderItem oi
INNER JOIN
Order o ON o.OrderId = oi.OrderId
WHERE
o.CategoryId = 52 -- website sales
GROUP BY
o.CustomerId
)
SELECT
cust.Code,
cust.DisplayTitle,
CTE.OrderCount,
CTE.OrderItemCount,
--AVG(CTE.OrderItemCount/CTE.OrderCount) AS SumProduct ????
FROM
CTE
INNER JOIN
Customer cust ON cust.CustomerId = CTE.CustomerId
GROUP BY
cust.Code,
cust.DisplayTitle,
CTE.OrderCount,
CTE.OrderItemCount
ORDER BY
SumProduct DESC
I'm basically trying to implement the T-SQL equivalent of SUMPRODUCT() in Excel.
SELECT
o.CustomerId,
COUNT(DISTINCT o.OrderId) AS OrderCount,
COUNT(oi.OrderItemId) AS OrderItemCount,
COUNT(oi.OrderItemId) / COUNT(DISTINCT o.OrderId) avg
FROM OrderItem oi
INNER JOIN Order o ON o.OrderId = oi.OrderId
WHERE o.CategoryId = 52 -- website sales
GROUP BY o.CustomerId
order by COUNT(oi.OrderItemId) / COUNT(DISTINCT o.OrderId) desc
Just add in the join to customer
I have this query:
SELECT city.CITY_NAME,
SUM(case when c.CUSTOMER_ID=o.CUSTOMER_ID and o.ORDER_ID=od.ORDER_ID
then od.TOTAL_AMT_PER_ITEM
else 0 end) AS TOTAL_AMT_PER_ITEM
FROM [ORDER] o
INNER JOIN ORDER_DETAILS od
ON o.ORDER_ID = od.ORDER_ID
INNER JOIN CUSTOMER c
ON o.CUSTOMER_ID = c.CUSTOMER_ID
INNER JOIN CUSTOMER_ADDRESS ca
ON ca.CUSTOMER_ID = c.CUSTOMER_ID
INNER JOIN CITY city
ON ca.CITY_ID = city.CITY_ID
GROUP BY city.CITY_NAME
I am a beginner in SQL SERVER. This query displays only the CITY_NAME that has a corresponding TOTAL_AMT_PER_ITEM value. What I need is to display all the CITY_NAMEs in the database even if their corresponding value is NULL. What is the work around for this? Can someone help me out? Thanks!
I change the order of the joins maybe that help.
You start with CITY because is the source for your GROUP BY and try to see if have any CUSTOMER_ADDRESS.
I guess if is a new store you can have 0 customers.
Then INNER JOIN because direction cant exist alone, they belong to a customer
Then LEFT JOIN because again a CUSTOMER may or may not have [ORDERS].
Then INNER JOIN because every [ORDERS] have [ORDER DETAILS]
Finally you SUM(od.TOTAL_AMT_PER_ITEM) from the last JOIN table, this can get some NULL's so you need include COALESCE
SELECT city.CITY_NAME,
COALESCE(SUM(od.TOTAL_AMT_PER_ITEM) , 0) as TOTAL_AMT_PER_ITEM
FROM [CITY]
LEFT JOIN [CUSTOMER_ADDRESS] ca
ON ca.CITY_ID = [CITY].CITY_ID
INNER JOIN CUSTOMER c
ON ca.CUSTOMER_ID = c.CUSTOMER_ID
LEFT JOIN [ORDER] o
ON o.CUSTOMER_ID = c.CUSTOMER_ID
INNER JOIN ORDER_DETAILS od
ON o.ORDER_ID = od.ORDER_ID
GROUP BY [CITY].CITY_NAME
btw you should change the name of the table [Order] to [Orders] because Order is a reserved word and can cause problems.
In general I rather use the plural name for tables because is an entity saving multiple of one type
CITIES instead of CITY
CUSTOMERS intead of CUSTOMER
ORDER_DETAILS is already plural, so try to keep consistence.
SELECT
city.CITY_NAME,
SUM(od.TOTAL_AMT_PER_ITEM) AS TOTAL_AMT_PER_ITEM
FROM
CUSTOMER c
INNER JOIN
CUSTOMER_ADDRESS ca
ON ca.CUSTOMER_ID = c.CUSTOMER_ID
INNER JOIN
CITY city
ON ca.CITY_ID = city.CITY_ID
LEFT JOIN
[ORDER] o
ON o.CUSTOMER_ID = c.CUSTOMER_ID
LEFT JOIN
ORDER_DETAILS
ON o.ORDER_ID = od.ORDER_ID
GROUP BY city.CITY_NAME
I have 3 tables .
1.Customers Table(CustomerID,CustomerName)
2.SalesTable(SalesChannel,CustoemrID,SalesID)
3.TransactionTable(SalesID,UnitsSold,TotalAmount,SellingDate)
Now i need the Total units sold and total amount for every month of each customer?
Please help me with this situation?
SELECT C.CustomerID
,C.CustomerName
,MONTH(t.SellingDate) AS SalesMonth
,SUM(t.UnitsSold * t.TotalAmount) AS TotalSales
FROM Customers C
INNER JOIN SalesTable S ON S.CustomerID = C.CustomerID
INNER JOIN TransactionTable t ON t.SalesID = S.SalesID
GROUP BY C.CustomerID ,C.CustomerName ,MONTH(t.SellingDate)
I have four tables, but for this I only need three tables, and I want to display the customerid, orderid, productid, quantity and the total cost which isn't in any table but need to calculate? so far I have managed to get it to display total cost for one order with the order id, but I want it to display all the columns mentioned above
Order:
order_id(primary key)
customer_id( foreign key)
orderline:
orderid(fk) + productid(fk) (pk)
quantity
product:
productid(pk)
price
What I have done is
select orderid, sum(rowcost) as totalcost
from (select o.quantity, o.productid, o.orderid, os.customerid, p.price,
(o.quantity * p.price) as rowcost
from orderline o
inner join order os
on o.orderid = os.orderid
inner join product p
on p.productid = o.productid
where productid = 123)
group by orderid;
Now I want it to display all the orderids along with the productid, customerid, totalcost, orderid and quantity. The list should follow customerid order.
How would I do this?
when I add more variables in the select, it gives me errors. I have tried many ways, none of them worked.
do you mean you want something like this:
select o.quantity, o.productid, o.orderid, os.customerid, p.price,
(o.quantity * p.price) as rowcost,
sum(o.quantity * p.price) over (partition by os.customerid) as totalcost
from orderline o
inner join order os
on o.orderid = os.orderid
inner join product p
on p.productid = o.productid
where p.productid = 123
or this, to keep the sum correct if you wanted to filter afterwards on a product
select *
from (select o.quantity, o.productid, o.orderid, os.customerid, p.price,
(o.quantity * p.price) as rowcost,
sum(o.quantity * p.price) over (partition by os.customerid) as totalcost
from orderline o
inner join order os
on o.orderid = os.orderid
inner join product p
on p.productid = o.productid)
where productid = 123--or just remove this where clause
fiddle: http://sqlfiddle.com/#!4/3103d/1