Does this SQL statement require a nested SELECT query? - sql

Here is what I have so far but the results are wrong.
SELECT c.CompanyName,
COUNT(o.OrderID) AS [Total Orders],
SUM(
(od.UnitPrice -
(od.UnitPrice * od.Discount))* Quantity) AS [Purchase Total]
FROM Customers AS c,
Orders AS o,
[Order Details] AS od
WHERE c.CustomerID = o.CustomerID
AND o.OrderID = od.OrderID
GROUP BY c.CompanyName
ORDER BY c.CompanyName;
The issue I am having is with the count, it is off by double or more. I believe that this is because the OrderID appears multiple times in the Order Details table. I think I need a nested SELECT statement but I am unsure how to do that.
Would I be removing the SUM() expression, Order Details, and the AND clause from the first query? Or am I way off?
With help I have gotten the COUNT field to work but now my SUM field is wrong. This is my most recent attempt and it produces the same value for every customer.
SELECT c.CompanyName,
COUNT(o.OrderID) AS [Total Orders],
(SELECT SUM(
(odIN.UnitPrice -
(odIN.UnitPrice * odIN.Discount)) * odIN.Quantity) AS [OrderTotal]
FROM [Order Details] AS odIN, Orders As oIN
WHERE odIN.OrderID = oIN.OrderID) AS [Purchase Total]
FROM
Customers AS c, Orders AS o
WHERE c.CustomerID = o.CustomerID
GROUP BY c.CompanyName
ORDER BY c.CompanyName;
I was unsuccessful at getting the query to fully work the way I wanted it to. Then I realized that maybe maybe I was looking for the wrong data. So I switched the name for the COUNT field to Num Products Purchased.
I would still like to get the other way working, but I think that will require creating a temporary table or view that could be used to do one of the calculations and then call it from the query. That is something I'll have to figure out.
Thank you for the attempts to help.

Because Access doesn't have COUNT(DISTINCT) then you need to create an inner query.
What this does is compute the sum of each item in an order in the inner query, and then sums up all the order totals for the customer as the purchase total. An individual OrderID will not be counted twice, as o and od now have a one to one relationship.
There might be a syntax error in there somewhere, but the idea should work.
SELECT c.CompanyName,
COUNT(o.OrderID) AS [Total Orders],
SUM(od.OrderTotal) AS [Purchase Total]
FROM
Customers AS c,
Orders AS o,
(SELECT odIn.OrderID,
SUM(
(odIn.UnitPrice -
(odIn.UnitPrice * odIn.Discount)) * odIn.Quantity) AS [OrderTotal]
FROM [Order Details] AS odIn
GROUP BY odIn.OrderID) AS od
WHERE c.CustomerID = o.CustomerID
AND o.OrderID = od.OrderID
GROUP BY c.CompanyName
ORDER BY c.CompanyName;

if the problem is because OrderID appears multiple times, try:
SELECT c.CompanyName, COUNT(DISTINCT o.OrderID) AS [Total Orders], SUM((od.UnitPrice - (od.UnitPrice * od.Discount)) * Quantity) AS [Purchase Total]
FROM Customers AS c, Orders AS o, [Order Details] AS od
WHERE c.CustomerID = o.CustomerID AND o.OrderID = od.OrderID
GROUP BY c.CompanyName
ORDER BY c.CompanyName;
The distinct clause lets you count only each appearance.

First I would like to thank Daniel for his help. I did finally get the query to work with help from another source. Daniel's solution is better as it requires less code and even formats the sum in currency.
Here is the one I got to work:
SELECT vtOrdCnt.*, ROUND(vtTotCost.PurchaseTotal, 2) AS [Purchase Total]
FROM
(
SELECT CustomerID, COUNT(OrderID) AS TotalOrders
FROM orders
GROUP BY CustomerID
) AS vtOrdCnt,
(
SELECT CustomerID, SUM(UnitPrice * (1-Discount)*Quantity) AS PurchaseTotal
FROM Orders AS o, [Order Details] AS od
WHERE o.orderID = od.orderID
GROUP BY CustomerID
) AS vtTotCost
WHERE vtOrdCnt.CustomerID = vtTotCost.CustomerID
ORDER BY vtOrdCnt.CustomerId
By using Aliases and two select statements in the FROM clause it allowed the query to function the way I wanted it to.

Related

I'm trying to use an aggregate function in order to consolidate the data but I'm not sure what I'm doing wrong

I have to find the total price per unit quantity and price. How can I make this work in a way where I can consolidate all the orders from one person with the total price that they ordered?
I'm new to SQL and not sure where to start to fix this.
SELECT DISTINCT Orders.OrderID, Customers.ContactName, Orders.OrderDate, [Order
Details].Quantity * [Order Details].UnitPrice
FROM Orders, Customers, [Order Details]
WHERE Orders.CustomerID = Customers.CustomerID
AND Orders.ShipCountry = 'Spain'
Three important things I can advice you to do:
Use explicit JOIN syntax: don't write your tables separately with comma, use explicit JOINs against them so it forces you to write their link at that moment and it's easier to read later. Your attempt was missing a link between the orders and the order details, probably making a lot of duplicates on your results.
Aggregates needs a GROUP BY. Whenever you want to apply aggregate functions like SUM, MAX, COUNT, you will (mostly) need a set of columns you want to group together. These columns go in the GROUP BY clause.
Don't use DISTINCT if you are unsure of the results you are getting. It seems that you used DISTINCT to cover up the missing link between orders and order details, so it just list uniques instead of the actual data your select is returning.
SELECT
C.CustomerID,
C.ContactName,
O.OrderID,
O.OrderDate,
TotalPrice = SUM(D.Quantity * D.UnitPrice)
FROM
Orders AS O
INNER JOIN Customers AS C ON O.CustomerID = C.CustomerID
INNER JOIN [Order Details] AS D ON O.OrderID = D.OrderID
WHERE
O.ShipCountry = 'Spain'
GROUP BY
C.CustomerID,
C.ContactName,
O.OrderID,
O.OrderDate
I think you want to do the join like this...
SELECT DISTINCT Orders.OrderID, Customers.ContactName, Orders.OrderDate,
[Order Details].Quantity * [Order Details].UnitPrice FROM Orders left join
Customers on Orders.CustomerID = Customers.CustomerID left join [Order
Details] on Orders.CustomerID = [Order Details].CustomerID where
Orders.ShipCountry = 'Spain'

Access Query from multiple tables

I currently have an Access Database with a few tables, among them Order, OrderDetails and Client.
Order (OrderID, TimeStamp, FKEmployeeID, FKClientID, OrderStatus, Comments)
OrderDetails (OrderDetailsID, FKOrderID, FKProductID, Quantity, Cost, Total Cost)
Client (ClientID, Name)
I'm trying to build a query where I can get the total orders that a client has made and the total Items.
Example:
Customer, Total Orders, Total Items
John, 5, 15
Alex, 2, 30
Ana, 1, 3
Whenever I try to make a query Total Orders and Total Items give me the same number.
Any help would be greatly appreciated!
This is supported even by ms access:
SELECT c.Name,
(select count(*)
from Orders o
where o. FKClientID = C.ClientID) as [Total Orders],
(select sum(Quantity) as Items
from OrderDetails od
inner join Order o on o.OrderID = od.FKOrderID
where o.FKClientId = C.ClientID) as [Total Items]
from Client c;
Unfortunately, MS Access doesn't support COUNT(DISTINCT). You can do this with two aggregations:
SELECT c.[Name], COUNT(*) As NumOrders, SUM(o.NumItems) As NumItems
FROM Client as c INNER JOIN
(SELECT o.OrderID, o.FKClientID, COUNT(*) As NumItems
FROM [Order] as o INNER JOIN
OrderDetails as od
ON od.FKOrderID = o.OrderID
GROUP BY o.OrderID, o.FKClientID
) as o
ON o.FKClientID = c.ClientId
GROUP BY c.ClientId, c.Name;
What about this solution (clients without orders are left out hereby):
SELECT Client.[Name],
Count(myTotalItems.OrderID) As TotalOrders,
Sum(myTotalItems.TotalItems) As TotalItems
FROM Client,
(SELECT First([Order].OrderID) As OrderID,
First([Order].FKClientID) As
ClientID,
Count(OrderDetails.OrderDetailsID) As TotalItems
FROM [Order], OrderDetails
WHERE OrderDetails.FKOrderID like [Order].OrderID
GROUP BY [Order].OrderID) As myTotalItems
WHERE myTotalItems.ClientID like Client.ClientID
GROUP BY Client.[Name];
It would be easier for you if you divide the task into multiple queries. I am giving the sample using Northwind database, you can test and see on it. Note that the structures and fieldnames are very similar to yours.
First create one that gets ClientId, OrderId, OrderDate and Sum of Quantity.
SELECT c.CustomerId, o.OrderId, o.OrderDate, sum(od.Quantity) AS Qty
FROM (Customers AS c INNER JOIN Orders AS o ON c.CustomerId = o.CustomerId)
INNER JOIN [Order Details] AS od ON o.OrderId = od.OrderID
GROUP BY c.CustomerId, o.OrderId, o.OrderDate;
Save this as "OrderOfClients" (it would be saved in Queries). Next create a query that uses this one and asks for date range:
SELECT c.CustomerId, c.CompanyName,
Count(*) AS [Total Orders],
Sum(Qty) AS [Total Items]
FROM Customers AS c
INNER JOIN OrdersOfClients AS co ON c.CustomerId = co.CustomerId
WHERE co.OrderDate Between [#startDate] And [#endDate]
GROUP BY c.CustomerId, c.CompanyName;
You can save this one as "OrdersOfClientsSummary" and call for your report.
PS: In my personal opinion, if you use a database other than access you would be doing yourself a big favor.

sql use aggregate function that counts a unique value with group by using inner joins

I searched and found similar questions online but not my particular one, they all use where or having clause.If theres one similar to mine please link it. It's a 2 part question and I have the first one done. Thank you in advance.
Okay so heres the question, part 1
"Find by customer, the total cost and the total discounted cost for each product on the order ?".
It also asks to use inner joins to find the customer and order it a specific way. Below is the answer.
SELECT
C.companyname, O.orderid, O.orderdate, P.productname,
OD.orderid, OD.unitprice, OD.qty, OD.discount,
(OD.unitprice * OD.qty - (OD.qty * OD.discount)) AS TotalCost,
(OD.qty * OD.discount) AS TotalDiscountedCost
FROM
Sales.Customers AS C
INNER JOIN
Sales.Orders AS O ON C.custid = O.custid
INNER JOIN
Sales.OrderDetails OD ON O.orderid = OD.orderid
INNER JOIN
Production.Products as P ON OD.productid = P.productid
ORDER BY
C.companyname, O.orderdate;
Now the second question is to
follow up and resume the first one by "customer and the order date year, the total cost and the total discounted cost on the order ?". It also asks for this, "Project following columns in the select clause as.
GroupByColumns.companyname
GroupByColumns.OrderdateYear
AggregationColumns.CountNumberOfIndividualOrders
AggregationColumns.CountNumberOfProductsOrders
AggregationColumns.TotalCost
AggregationColumns.TotalDiscountedCost
Finally to order by company name and orderdateYear( which are groups). Where im stuck is how to count the specific orders of qty that equal 1 as an aggregate function in the SELECT clause. I know it has to use the aggregate function COUNT because of the GROUP BY, just don't know how to. This is what I have.
SELECT
C.companyname, YEAR(O.orderdate) AS orderyear,OD.qty,
-- Where in the count function or if theres another way do I count all the
--single orders
--COUNT(OD.qty) AS indiviualorders,
(OD.unitprice * OD.qty - (OD.qty * OD.discount)) AS TotalCost,
(OD.qty * OD.discount) AS TotalDiscountedCost
FROM
Sales.Customers AS C
INNER JOIN
Sales.Orders AS O ON C.custid = O.custid
INNER JOIN
Sales.OrderDetails OD ON O.orderid = OD.orderid
INNER JOIN
Production.Products as P ON OD.productid = P.productid
GROUP BY
C.companyname, YEAR(O.orderdate)
ORDER BY
C.companyname, O.orderdate;
You case use a case statement inside a sum
SUM(CASE WHEN <xyz> THEN 1 ELSE 0 END)
But for the count of unique orders, use SELECT(DISTINCT ) on a key that is unique in the order table
SELECT COUNT(DISTINCT O.OrderID) As DistinctOrders FROM Table

Trying to sum part of the rows

So I have three tables
Orders:
CustomerID,OrderID
Order Details;
OrderID,ProductId,UnitPrice,Quantity,Discount
And Products:
ProductID,ProductName
And I need to combine these 2 tables and create this one:
[Orderd Details].CustomersID,Products.ProductName,FORMULA
Formula is how much money people spent money on this product. So I think I have to sum UnitPrice* Quantity*(1-Discount) from every order for this product.
Sadly I have no idea how should I do it. The best I did is:
SELECT o.CustomerID,p.ProductName,SUM(od.Quantity*od.UnitPrice*(1-od.Discount)) as 'SKZ'
FROM Customers as c, Orders as o,[Order Details] as od,Products as p
WHERE (o.OrderID=od.OrderID AND p.ProductID=od.ProductID)
GROUP BY od.ProductID ORDER BY o.CustomerID;
But it doesn't work.
First, learn explicit JOIN syntax. Simple rule: Never use commas in the FROM clause.
Second, you should include all non-aggregated columns in the GROUP BY:
SELECT o.CustomerID, p.ProductName,
SUM(od.Quantity * od.UnitPrice * (1 - od.Discount)) as SKZ
FROM Orders as o JOIN
[Order Details] od
ON o.OrderID = od.OrderID JOIN
Products p
ON p.ProductID = od.ProductID
GROUP BY o.CustomerID, p.ProductName
ORDER BY o.CustomerID;

SQL Server query for total dollar amount of orders per customerID

I have two tables that I need to create a query for. I'm sure I'll need to join them as well.
I need to take the CustomerName and Address of customer from my Customer table but I also need the query to show the total dollar amount of all the orders placed by customer. So if the customer spent for example $300 dollars this year in total, that $300 is the output I'm trying to achieve.
I have a table called Order Details that uses an OrderID which is tied to a CustomerID and there are unitprice and quantity columns in the Order Details table. I'm trying to figure out how to multiple these but I am going crazy.
I have tried this to get at least the total from the orders but I have syntax errors for sure:
SELECT unitprice,
quantity
FROM [Order details] (unitprice * quantity) AS Totalorders,
from [Order Details]
WHERE orderid > 0
also this without any luck :
SELECT customers.companyname AS 'Company Name',
customers.address AS 'Address',
[order details].unitprice * [order details].quantity AS 'Orders'
FROM customers
LEFT JOIN orders
ON customers.customerid = orders.customerid
ORDER BY customers.companyname,
customers.address,
orders
Thanks
You need to join in the order details and do a group by aggregation:
SELECT c.CompanyName AS "Company Name",
c.Address AS "Address",
sum(od.Unitprice * od.quantity) as "Orders"
FROM Customers c LEFT JOIN
Orders o
ON c.CustomerID = o.CustomerID left join
[Order Details] od
on od.orderid = o.orderid
GROUP BY c.CompanyName, c.Address
ORDER BY c.CompanyName, c.Address
In addition, I made some stylistic changes. I added aliases to the table names. I find it much easier to read c.CompanyName rather than Customers.CompanyName. I changed the delimiter on the column aliases to use double quotes rather than single quotes. I associate single quotes with string constants inside the statement.
if it s one-to-many relationship, your query will look something like:
SELECT
Customers.CompanyName AS 'Company Name',
Customers.Address AS 'Address',
SUM(O.Unitprice * O.quantity) as 'Orders'
FROM Customers
LEFT JOIN [Order Details] O
ON Customers.CustomerID = O.CustomerID
GROUP BY Customers.CompanyName, Customers.Address
ORDER BY Customers.CompanyName, Customers.Address