Good afternoon Stack Overflow community! I'm pretty new to sql and writing queries, so if someone could just double check my work and maybe pass along any helpful tips at how I might go about coding it better? I've pasted along the purpose of the query and my code - I appreciate the help and feedback thanks!
Basically the query should create a report of customers living in Europe who have recently placed an order totaling more than $5,000. Order total must reflect the quantity of the products the customer ordered, which should also have a discount and order total.
Thanks again for the help!
select c.CustomerID, c.Country, sum (od.Discount * od.UnitPrice)'Total'
from Customers c
inner join Orders o
on c.CustomerID = o.CustomerID
inner join OrderDetails od
on o.OrderID = od.OrderID
where c.Country in ('germany', 'UK', 'sweden', 'france', 'spain', 'switzerland','austria', 'italy', 'belgium', 'norway', 'denmark', 'finland', 'poland')
group by c.Country, c.CustomerID
having sum ('Total' * od.Quantity) <= 5000
order by total desc
It looks good, but for a couple of points:
1) Your Total may be wrong
2) You must reverse the comparison operator:
select c.CustomerID, c.Country, sum (od.Discount * od.UnitPrice * od.Quantity) 'Total'
from Customers c
inner join Orders o
on c.CustomerID = o.CustomerID
inner join OrderDetails od
on o.OrderID = od.OrderID
where c.Country in ('germany', 'UK', 'sweden', 'france', 'spain',
'switzerland','austria', 'italy', 'belgium', 'norway', 'denmark',
'finland', 'poland')
and o.OrderDate >= '2017-03-01' -- for example
group by c.Country, c.CustomerID
having sum ( od.Discount * od.UnitPrice * od.Quantity) >= 5000
order by total desc
Related
I am trying to determine the customers spending behavior by writing a query to select the top 10 highest spenders.
I have the following query and it works fine:
SELECT c.CustomerID
, SUM(Quantity * UnitPrice) Total_Spent
FROM Orders o
JOIN OrderDetails d
ON d.OrderID = o.OrderID
JOIN Customers c
ON c.CustomerID = o.CustomerID
GROUP
BY c.CustomerID
ORDER
BY Total_Spent
limit 10;
However, I want to create a subquery instead of having this complicated one. I have tried the following but it doesn't work:
SELECT Customers.CustomerID
FROM Customers
JOIN (
SELECT SUM(Quantity *UnitPrice) as Total_Spent
FROM Orders
JOIN OrderDetails
ON Orders.OrderID = OrderDetails.OrderID) Orders
ON Orders.CustomerID = Customers.CustomerID
GROUP BY Customers.CustomerID
ORDER BY Total_Spent
LIMIT 10;
I don't know what the problem is. (I am kinda new in SQL)
You can simplify your query because you don't need the customers table. That and table aliases should make your query simpler:
SELECT o.CustomerID, SUM(od.Quantity * od.UnitPrice) as Total_Spent
FROM Orders o JOIN
OrderDetails od
ON o.OrderID = od.OrderID
GROUP BY o.CustomerID
ORDER BY Total_Spent DESC
LIMIT 10;
I am having trouble with the syntax of this query. I am trying to return the largest total for one company from each country.
The tables look like:
Orders
Columns
OrderID
CustomerID
EmployeeID
OrderDate
RequiredDate
OrderDetails
Columns
OrderID
ProductID
UnitPrice
Quantity
Products
Columns
ProductID
ProductName
QuantityPerUnit
UnitPrice
Customers
Columns
CustomerID
CompanyName
ContactName
Country
I have tried the following:
SELECT
T1.Country,
CompanyName,
T1.OrderSum
FROM
(SELECT
C.Country,
C.CompanyName,
SUM(UnitPrice * Quantity) AS OrderSum
FROM Customers C
JOIN Orders O
ON C.CustomerID = O.CustomerID
JOIN OrderDetails D
ON D.OrderID = O.OrderID
GROUP BY C.Country) T1
JOIN
-- TOP PAYMENT TOTALS BY COUNTRY
(SELECT COUNTRY,
MAX(OrderSum) AS OrderSum
FROM
-- PAYMENT TOTALS BY CUSTOMER
(SELECT C.Country,
C.CompanyName,
SUM(UnitPrice * Quantity) AS OrderSum
FROM Customers C
JOIN Orders O1
ON O1.CustomerID = C.CustomerID
JOIN OrderDetails D1
ON D1.OrderID = O1.OrderID
GROUP BY C.COUNTRY, C.CompanyName) T2
GROUP BY COUNTRY) T3
ON T1.COUNTRY = T3.COUNTRY
AND T1.OrderSum = T3.OrderSum
ORDER BY Country;
This query only returns three countries:
Ireland Hungry Owl All-Night Grocers 57317.3900
Norway Sant Gourmet 5735.1500
Poland Wolski Zajazd 3531.9500
But, this query I tried, returns all countries, but I am not sure if it is correct because I did not include the 'max' value as I did in the previous query:
SELECT
T1.Country,
CompanyName,
T1.OrderSum
FROM
(SELECT
C.Country,
C.CompanyName,
SUM(UnitPrice * Quantity) AS OrderSum
FROM Customers C
JOIN Orders O
ON C.CustomerID = O.CustomerID
JOIN OrderDetails D
ON D.OrderID = O.OrderID
GROUP BY C.CompanyName) T1
GROUP By Country
ORDER BY Country;
I am also unsure if I am calculating the order totals correctly, which may be the mistake on my part. But I am trying to find the company from each country that has the largest order total.
Sorry for all the text.
The following query would list only the companies with the largest order total per country:
SELECT A.Country, A.CompanyName, A.OrderSum
FROM (
SELECT
C.Country,
C.CompanyName,
SUM(D.UnitPrice * D.Quantity) AS OrderSum
FROM Customers C
JOIN Orders O ON C.CustomerID = O.CustomerID
JOIN OrderDetails D ON D.OrderID = O.OrderID
GROUP BY
C.Country, C.CustomerID
) A
JOIN (
SELECT
S.Country, MAX(S.OrderSum) as MaxSum
FROM (
SELECT
C.Country,
C.CompanyName,
SUM(D.UnitPrice * D.Quantity) AS OrderSum
FROM Customers C
JOIN Orders O ON C.CustomerID = O.CustomerID
JOIN OrderDetails D ON D.OrderID = O.OrderID
GROUP BY
C.Country, C.CustomerID
) S
GROUP BY
S.Country
) B ON A.Country = B.Country
WHERE
A.Country = B.Country AND
A.OrderSum = B.MaxSum
ORDER BY
A.Country, A.CompanyName
;
[UPDATE]
Note that the above SQL follows the way OrderSum is calculated in your listed queries. Given that table Products has QuantityPerUnit and UnitPrice, I suspect that your OrderSum should be multiplied by QuantityPerUnit as well – in which case you'll need to revise the math for OrderSum.
I have tables on my database with the following schema:
customers (customerID: integer, fName: string, lName: string)
items (itemID: integer, description: string, price: integer)
orders (orderID: integer, itemID: integer, aID: integer, customerID:integer, date: date)
and the following code:
SELECT c.customerID, COUNT(DISTINCT o.orderID) AS number_of_orders,
ROUND(SUM(i.price) / COUNT(DISTINCT o.orderID),2) AS average
FROM customers c
LEFT JOIN orders o
ON o.customerID = c.customerID
AND o.date >= '2013-03-01'
AND o.date < '2013-04-01'
LEFT JOIN items i
ON o.itemID = i.itemID
GROUP BY c.customerID
which returns three values: customer ID, number of orders per customer, and average spending per customer.
With the code as it is now, the average spending per customer is returned as blank (null).
I am having trouble using the IFNULL function to set the average spending per customer to 0.00 if the customer did not order anything in march 2013 (i.e., if number of orders per customer in march is zero).
Any help will be very much appreciated!
Without seeing your IFNULL attempts, this logic works for me:
SELECT c.customerID,
COUNT(DISTINCT o.orderID) AS number_of_orders,
ROUND(IFNULL( (SUM(i.price) / COUNT(DISTINCT o.orderID)), 0.00),2) AS average,
FORMAT(IFNULL(ROUND( (SUM(i.price) / COUNT(DISTINCT o.orderID)),2), 0), 2) AS averageWithFormat
FROM customers c
LEFT JOIN orders o ON o.customerID = c.customerID AND o.date >= '2013-03-01' AND o.date < '2013-04-01'
LEFT JOIN items i ON o.itemID = i.itemID
GROUP BY c.customerID
Returns: 0.00
I have two entries for average in my statement because in my local MySQL Workbench, the first one returns with two decimal precision but doesn't in SQLFiddle. I added the second entry with the FORMAT syntax to force the two decimal precision, if needed.
You use case maybe better off using the IF() function.
SELECT c.customerID, COUNT(DISTINCT o.orderID) AS number_of_orders,
IF(COUNT(o.orderID) > 0,ROUND(SUM(i.price) / COUNT(DISTINCT o.orderID),2),0) AS average
FROM customers c
LEFT JOIN orders o
ON o.customerID = c.customerID
AND o.date >= '2013-03-01'
AND o.date < '2013-04-01'
LEFT JOIN items i
ON o.itemID = i.itemID
GROUP BY c.customerID
The following code shows all our customers that have made purchases since our database was created and how much money they have spent. There are a couple customers that have not made purchases since we implemented the new database but they are not appearing when I run this code. I have looked around this site for similar examples but the solutions are too complex for me.
There is also a customers table that shows the results for all of our 'n' customers, that table connects to the orders table through customerID. Not sure if that will help.
select t3.CustomerID, sum(Revenue) as Revenue
from
(
select orderid, sum(UnitPrice*quantity) as Revenue from [Order Details]
group by OrderID
)t1
inner join
(
select customerid,orderid from orders
)t3
on t1.orderid=t3.orderid
group by t3.CustomerID
I think you just want this rather simpler query:
select c.CustomerID, sum(od.UnitPrice * od.quantity) as Revenue
from customers c left outer join
orders o
on o.CustomerId = c.CustomerId left outer join
`Order Details` od
on od.OrderId = o.OrderId
group by c.CustomerID;
I am working on an mySQL assignment for school and I am stuck on a question. I am still new to mySQL. COUNT(o.customer_id) is not working the way I want. I want it to count the number of orders but it is counting all items. i.e. Customer 1 has 2 orders but it is returning 3 because one order has two items. I have three tables one with customers, another with orders than another with each item on each order. Ive posed my query below. Any help would be great.
SELECT email_address, COUNT(o.order_id) AS num_of_orders,
SUM(((item_price - discount_amount) * quantity)) AS total
FROM customers c JOIN orders o
ON c.customer_id = o.customer_id
JOIN order_items ot
ON o.order_id = ot.order_id
GROUP BY o.customer_id
HAVING num_of_orders > 1
ORDER BY total DESC;
As simple as use Distinct reserved word:
SELECT email_address, COUNT(distinct o.order_id) AS num_of_orders
Looks like you want to count the DISTINCT number of orders. Add a DISTINCT into the COUNT. Although MySQL allows you to use the SELECT expression in the HAVING clause, it's not good practice to do so.
SELECT email_address, COUNT(DISTINCT o.order_id) AS num_of_orders,
SUM(((item_price - discount_amount) * quantity)) AS total
FROM customers c JOIN orders o
ON c.customer_id = o.customer_id
JOIN order_items ot
ON o.order_id = ot.order_id
GROUP BY o.customer_id
HAVING COUNT(DISTINCT o.order_id) > 1
ORDER BY total DESC;
Just take out the join to items. All it is doing is duplicating rows when there are multiple items.
SELECT email_address, COUNT(o.order_id) AS num_of_orders,
SUM(((item_price - discount_amount) * quantity)) AS total
FROM customers c JOIN orders o
ON c.customer_id = o.customer_id
GROUP BY o.customer_id
HAVING COUNT(o.order_id) > 1
ORDER BY total DESC;