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.
Related
I currently am trying to write a query that shows customers with at least 5 orders and customer with no orders. Orders are tracked in their own table and in order to find customers with 0 orders we have to find the customers NOT IN orders. Below is my query I'm trying to use and it returns the same customer 5 times for zero orders.
with t1 as
(select o.customerNumber, c.customerName, count(o.orderNumber) as FiveOrders
from orders o join customers c on (o.customerNumber = c.customerNumber)
group by o.customerNumber having count(o.orderNumber) = 5),
t2 as
(select distinct o.customerNumber, c.customerName, count(o.orderNumber) as NoOrders
from orders o join customers c on (o.customerNumber = c.customerNumber)
group by c.customerNumber not in(select customerNumber from orders))
select distinct t1.customerNumber as FiveOrderNumber, t1.customerName as FiveOrderName,
t2.customerNumber as NoOrderNumber, t2.customerName as NoOrderName
from t1 join t2
order by NoOrderName;
Any and all help is appreciated thanks!
If the errors were only in the second table to, I think it is after using
having with condition NOT IN without any logical comparison, I think you can get wanted results easily like:
select distinct customerNumber, customerName, "0" as NoOrders
from customers
where customerNumber not in (Select customerNumber from orders)
If the group by is important, you can use it like in your code.
Zero or five could be counted together with LEFT JOIN
select c.customerNumber, max(c.customerName) customerName, count(o.orderNumber) as FiveOrdersOrZero
from customers c
left join orders o on o.customerNumber = c.customerNumber
group by c.customerNumber
having count(o.orderNumber) in ( 0, 5 )
order by FiveOrdersOrZero
I am trying to sum the PRICE from two tables which are connected by another table.
tblcustomer tblappointment tblfinances
---------- -------------- -----------
CustomerID AppointmentID FinancesID
Name CustomerID CustomerID
Price Price
I am trying this code but I am getting an error #1064
SELECT c.CustomerID, (1.p1+2.p2) AS total_price FROM tblcustomer c
LEFT JOIN (SELECT CustomerID, SUM(Price) p1 FROM tblappointment GROUP BY CustomerID) 1 ON 1.CustomerID = c.CustomerID
LEFT JOIN (SELECT CustomerID, SUM(Price) p2 FROM tblfinances GROUP BY CustomerID) 2 ON 2.CustomerID = c.CustomerID;
I do not recommend using aliases that are or start with numbers. More importantly, you need to recognize that the JOINs may not match -- that would be why you are using outer joins in the first place.
So:
SELECT c.CustomerID,
(COALESCE(a.price, 0) + COALESCE(f.price, 0)) AS total_price
FROM tblcustomer c LEFT JOIN
(SELECT CustomerID, SUM(Price) as price
FROM tblappointment
GROUP BY CustomerID
) a
ON a.CustomerID = c.CustomerID LEFT JOIN
(SELECT CustomerID, SUM(Price) as price
FROM tblfinances
GROUP BY CustomerID
) f
ON f.CustomerID = c.CustomerID;
You need to fix aliasing as follows
SELECT c.CustomerID, (t1.p1+t2.p2) AS total_price
FROM tblcustomer c
LEFT JOIN (SELECT CustomerID, SUM(Price) as p1
FROM tblappointment
GROUP BY CustomerID) t1 ON t1.CustomerID = c.CustomerID
LEFT JOIN (SELECT CustomerID, SUM(Price) as p2
FROM tblfinances
GROUP BY CustomerID) t2 ON t2.CustomerID = c.CustomerID;
since aliases can not be totally numeric expressions except they're quoted
I am trying to return the CustomerID, CompanyName, OrderID, and subtotals for each customer for all order the subtotal amounts that are higher than the customer’s average subtotal amount. These are the tables I am using and the query below. I am unsure if the values I return are correct and was hoping someone could help me understand if they are or not based on my query. Thanks in advance.
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
SELECT A.CustomerID, A.CompanyName, A.Subtotal, A.OrderID, AVGSubtotal
FROM (
SELECT
C.CustomerID,
C.CompanyName,
(D.UnitPrice * P.QuantityPerUnit) AS Subtotal,
D.OrderID
FROM Customers C
JOIN Orders O ON C.CustomerID = O.CustomerID
JOIN OrderDetails D ON D.OrderID = O.OrderID
JOIN Products P ON P.ProductID = D.ProductID
GROUP BY
D.OrderID, C.CustomerID
) A
JOIN (
SELECT
S.CustomerID, S.CompanyName, AVG(S.Subtotal) as AVGSubtotal
FROM (
SELECT
C.CustomerID,
C.CompanyName,
(D.UnitPrice * P.QuantityPerUnit) AS Subtotal
FROM Customers C
JOIN Orders O ON C.CustomerID = O.CustomerID
JOIN OrderDetails D ON D.OrderID = O.OrderID
JOIN Products P ON P.ProductID = D.ProductID
GROUP BY
D.OrderID, C.CustomerID
) S
GROUP BY
S.CustomerID
) B ON A.CustomerID = B.CustomerID
WHERE
A.CustomerID = B.CustomerID AND
A.Subtotal > B.AVGSubtotal
ORDER BY
A.CustomerID, A.CompanyName
;
select
c2.customerID,
c2.CompanyName,
c2.AVGSubtotal
o2.OrderID,
o2.UnitPrice * o2.Quantity as subtotal
from (
select
c.CustomerID,
c.CompanyName,
sum(o.UnitPrice * o.Quantity)/count(*) as AVGSubtotal
from
Customers c
inner join Orders o on (o.CustomerID = c.CustomerID)
inner join OrderDetails od on (od.OrderID = c.OrderID)
group by
o.CustomerID
) as c2
inner join Orders o2 on (o2.CustomerID = c2.CustomerID)
where o2.UnitPrice * o2.Quantity > c2.AVGSubtotal
I have a to write an SQL query to list the names and total spend of customers who made more
than one order. The following lists all the relevant information but I'm struggling to see how to move forward with this.
SELECT l.quantity, o.orderID, i.itemID, o.custID, i.unitcost, c.familyname
FROM lineitems l, orders o, items i, customers c
WHERE l.itemID = i.itemID
AND c.custID = o.custID
AND o.orderID = l.orderID
ORDER BY o.custID
Select customerId, Sum(i.quantity*i.unitCost)
From lineitems I
join orders o on o.orderID = i.orderID
where Exists(Select * From orders
where customerId = o.customerId
having count(*) > 1)
group by customerId
or, with name instead of just customerId
Select c.familyname, Sum(i.quantity*i.unitCost)
From lineitems I
join orders o on o.orderID = i.orderID
join customers c on c.customerId = o.customerId
where Exists(Select * From orders
where customerId = o.customerId
having count(*) > 1)
group by c.familyname
I am new to database field. I am currently learning mysql using Murach's Mysql. I came across following problem in the book but I am not able to figure out the correct query for solving it.
Write a SELECT statement that
joins the
Customers, Orders, Order_Items, and
Products tables. This statement should return these columns:
last_name,
first_name,
order_date, product_na
me, item_price, discount_amount,
and
quantity
.
Use aliases for the tables.
Sort the final result set by
last
_name,
order
_date, and
product_name
.
By far I have this query:
select last_name , first_name , order_date , product_name , tem_price,
discount_amount, quantity
from customers , orders , order_items product
order by last_name , order_date , product_name
You can use inner join for these 4 tables Customers , Orders, Order_items , Products but make sure there is a match between the columns in these 3 tables.
Example of the sql query based on the given information. (Assume CustomerID is a match between the columns)
Select A.last_name,A.first_name,B.order_date,B.product_name,C.item_price,C.discount_amount,D.quantity
from TB_Customers A inner join TB_Orders B on A.CustomerID = B.CustomerID
inner join TB_Order_Items C on B.CustomerID = C.CustomerID on
inner join TB_Products D on C.CustomerID = D.CustomerID Where D.CustomerID ='TEST111'
Using ANSI standard join syntax:
SELECT c.last_name, c.first_name, o.order_date, p.product_name,
p.item_price, p.discount_amount, p.quantity
FROM Customers AS c
INNER JOIN Orders AS o ON c.order_id = o.order_id
INNER JOIN Order_Items AS i ON o.order_id = i.order_id
INNER JOIN Products AS p ON i.product_id = p.product_id
ORDER BY c.last _name, o.order _date, p.product_name
Well, we are going to analize the query. Since you didn't specify the database structure, I'm going to do an aproaching.
"SELECT statement that joins the Customers, Orders, Order_Items, and Products tables."
This is refered to the tables, this is in the FROM clause. You have to join them with WHERE clause.
SELECT *
FROM Customers, Orders, Order_Items, Products
WHERE Customers.order_id = Orders.order_id
AND Order_Items.order_id = Orders.order_id
AND Products.product_id = Order_Items.product_id
"This statement should return these columns: last_name, first_name, order_date, product_na me, item_price, discount_amount, and quantity "
This part is refered to the SELECT clause.
SELECT last_name, first_name, order_date, product_name,
item_price, discount_amount, quantity
FROM Customers, Orders, Order_Items, Products
WHERE Customers.order_id = Orders.order_id
AND Order_Items.order_id = Orders.order_id
AND Products.product_id = Order_Items.product_id
"Use aliases for the tables"
This part involves the whole query and means that have to rename the tables (virtually and only form this query) and in every reference you have to use the new name.
SELECT c.last_name, c.first_name, o.order_date, p.product_name,
p.item_price, p.discount_amount, p.quantity
FROM Customers AS c, Orders AS o, Order_Items AS i, Products AS p
WHERE c.order_id = o.order_id
AND i.order_id = o.order_id
AND p.product_id = i.product_id
"Sort the final result set by last _name, order _date, and product_name."
This part refered to th ORDER BY clause. You have to keep in mind the aliases
SELECT c.last_name, c.first_name, o.order_date, p.product_name,
p.item_price, p.discount_amount, p.quantity
FROM Customers AS c, Orders AS o, Order_Items AS i, Products AS p
WHERE c.order_id = o.order_id
AND i.order_id = o.order_id
AND p.product_id = i.product_id
ORDER BY c.last _name, o.order _date, p.product_name