I'm very new to sql, and Stack Overflow. I'm hoping someone can help assist me in this query. My query is supposed to display the total sales and total items sold for categories with more than $200,000 in sales. I've been working on this query for an hour and am at my wits end, and help is appreciated!
select distinct c.categoryname,
sum(p.unitprice * od.Quantity) as 'Sales'
from Categories c
inner join Products p
on c.CategoryID = p.CategoryID
inner join OrderDetails od
on p.ProductID = od.ProductID
where p.unitprice < 200000
group by c.categoryname
I'm hoping I'm at least on the right track, thanks for the help!
Try this version:
SELECT
c.categoryname,
sum(od.Quantity) as "Items Sold", -- you were missing this
sum(p.unitprice * od.Quantity) as "Sales"
FROM
Categories c
INNER JOIN Product p ON c.CategoryID = p.CategoryID,
INNER JOIN OrderDetails od on p.ProductID = od.ProductID
WHERE
sum(p.unitprice * od.Quantity) > 200000 -- filter on the sales, not product
GROUP BY
c.categoryname
Related
I am trying to solve the following query, there's a few additional parameters, but these are the main attributes required:
Provide the product details, which vendors supply these products, and what was the last date these products were ordered by customers.
I have my original query below which gets me 90% of the way. I just can't seem to figure out how to display the last order date by customer per individual product. I've tried embedding (select max(o.OrderDate) from orders as o) into my select statement, but it only displays the latest order date of all of the products, not the individual per product last order date (e.g., all dates listed are 01/01/2020 when I know other products' last order date was before this date).
Apologies, I do not have enough rep to post pictures in line with text, therefore I have attached pictures of table structure and my query.
SQL Query
Table structure
Query:
select distinct p.ProductNumber, p.ProductName, p.RetailPrice, p.QuantityOnHand, v.VendName,
(select max(o.OrderDate)
from orders as o)
as LastOrderDateByCust
from ((((orders as o
inner join order_details as od
on od.OrderNumber = o.OrderNumber)
inner join products as p
on p.ProductNumber = od.ProductNumber)
inner join product_vendors as pv
on p.ProductNumber = pv.ProductNumber)
inner join vendors as v
on pv.VendorID = v.VendorID)
where p.QuantityOnHand < '10'
order by LastOrderDateByCust DESC;`
figured it out on my own I believe:
select distinct p.ProductNumber, p.ProductName, c.CategoryDescription, p.RetailPrice, pv.WholesalePrice, p.QuantityOnHand, v.VendName, pv.DaysToDeliver, (select max(o.OrderDate)
from orders as o
inner join order_details as od
on o.OrderNumber = od.OrderNumber
where od.ProductNumber = p.ProductNumber)
as LastOrderDateByCust
from (((((orders as o
inner join order_details as od
on o.OrderNumber = od.OrderNumber)
inner join products as p
on p.ProductNumber = od.ProductNumber)
inner join product_vendors as pv
on p.ProductNumber = pv.ProductNumber)
inner join vendors as v
on pv.VendorID = v.VendorID)
inner join categories as c
on p.CategoryID = c.CategoryID)
where od.ProductNumber = p.ProductNumber and p.QuantityOnHand < '10'
order by p.ProductNumber;`
I'm trying to get the product that has the highest gross value per category. I get the correct 'Grossed' amount by category, but it doesn't seem to bring the correct 'ProductID' and 'ProductName' with it.
SELECT
c.CategoryName,
prodGross.ProductID,
prodGross.ProductName,
MAX(ROUND(Grossed, 2)) AS Grossed
FROM
categories AS c
JOIN
(SELECT
p.ProductID,
p.ProductName,
p.CategoryID,
SUM(((od.UnitPrice * od.Quantity) - ((od.UnitPrice * od.Quantity) * od.Discount))) AS Grossed
FROM
northwind.`order details` AS od
JOIN products AS p ON p.ProductID = od.ProductID
GROUP BY p.ProductID) AS prodGross ON prodGross.CategoryID = c.CategoryID
GROUP BY c.CategoryName;
Any feedback would be helpful.
Thank you!
Consider derived tables of both aggregate queries matching the CategoryID and Grossed fields:
SELECT p.*, c.CategoryName, ROUND(g.MaxGrossed) AS HighestGrossed
FROM category c
INNER JOIN
(SELECT subp.ProductID, subp.ProductName, subp.CategoryID,
SUM(((od.UnitPrice * od.Quantity) -
((od.UnitPrice * od.Quantity) * od.Discount))) AS Grossed
FROM northwind.`order details` AS od
INNER JOIN products AS subp ON subp.ProductID = od.ProductID
GROUP BY subp.ProductID) As p
ON c.CategoryID = p.CategoryID
INNER JOIN
(SELECT subg.CategoryID, MAX(subg.Grossed) AS MaxGrossed
FROM
(SELECT subp.ProductID, subp.ProductName, subp.CategoryID,
SUM(((od.UnitPrice * od.Quantity) -
((od.UnitPrice * od.Quantity) * od.Discount))) AS Grossed
FROM northwind.`order details` AS od
INNER JOIN products AS subp ON subp.ProductID = od.ProductID
GROUP BY subp.ProductID) AS subg
GROUP BY subg.CategoryID) AS g
ON p.CategoryID = g.CategoryID AND p.Grossed = g.MaxGrossed
You can even save the repeated aggregate that calculates Grossed as a separate stored view and reference it in this query:
SELECT p.*, c.CategoryName, ROUND(g.MaxGrossed) AS HighestGrossed
FROM category c
INNER JOIN ProdGrossedView As p
ON c.CategoryID = p.CategoryID
INNER JOIN
(SELECT v.CategoryID, MAX(v.Grossed) AS MaxGrossed
FROM ProdGrossedView v
GROUP BY v.CategoryID) AS g
ON p.CategoryID = g.CategoryID AND p.Grossed = g.MaxGrossed
I need to display cust_id, the customer forename and surname, the product name<-(from products table) and date of sale<--(from sales table), also I need to display in order of the most recent dates first.
This is what I have got so far:
SELECT
customer.cust_id,
customer.forename,
customer.surname,
products.prod_name,
sales.Date_of_sale
FROM customers c
INNER JOIN sales s ON c.cust_id = s.cust_id
INNER JOIN products p ON s.product_id = p.product_id
ORDER BY s.Date_of_sale DESC
Any help would be appreciated.
This
SELECT
c.cust_id,
c.forename,
c.surname,
p.prod_name,
s.Date_of_sale
FROM customers c
INNER JOIN sales s ON c.cust_id = s.cust_id
INNER JOIN products p ON s.product_id = p.product_id
ORDER BY s.Date_of_sale DESC
will work
Hey guys I have an question that I need some support on.
I am trying to get the top 3 suppliers with a single query from a table.
This is the original question: Who are the top three suppliers by revenue, and where are they located?
Here is the online table and a query you have to run to create a new table.
http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
CREATE TABLE ByCustomerOrders AS
SELECT
o.OrderID
, p.ProductName
, p.ProductID
, Price
, Quantity
, Price * Quantity AS subtotal
, c.CustomerID
, s.SupplierID
FROM OrderDetails AS od
LEFT JOIN Orders AS o ON od.OrderID = o.OrderID
LEFT JOIN Products AS p ON p.ProductID = od.ProductID
LEFT JOIN Customers AS c on c.CustomerID = o.CustomerID
LEFT JOIN Suppliers AS s ON s.SupplierID = p.SupplierID;
From that, it creates a new table I need to list just the top 3 suppliers, pretty much the supplierID row value that shows up the most.
Some help would be appreciated.
If you want to get the 3 top suppliers by revenue (and revenue is the sum of all subtotals) this should work:
SELECT s.*, SUM(co.subtotal) as revenue
FROM ByCustomerOrders co
INNER JOIN Suppliers s ON co.SupplierID = s.SupplierID
GROUP BY co.SupplierID
ORDER BY revenue DESC
LIMIT 3;
PS: You should consider using decimal (instead of float or double) for columns that will represent money or you'll get precision errors and your numbers won't add up.
You have a fairly complex schema that you haven't completely disclosed, so this is a guess.
SELECT COUNT(s.SupplierID) AS supplier_count,
SUM(Price * Quantity) AS supplier_subtotal,
s.SupplierID,
s.SupplierName /*this is a guess*/
FROM OrderDetails AS od
LEFT JOIN Orders AS o ON od.OrderID = o.OrderID
LEFT JOIN Products AS p ON p.ProductID = od.ProductID
LEFT JOIN Customers AS c on c.CustomerID = o.CustomerID
LEFT JOIN Suppliers AS s ON s.SupplierID = p.SupplierID
GROUP BY s.SupplierID, s.SupplierName
ORDER BY COUNT(s.SupplierID) DESC
LIMIT 3
This should give you the top suppliers (by units ordered).
The trick here is to use an aggregate query (SUM() ... GROUP BY) and then order by one of the aggregate values with a DESCending qualifier.
You might want to troubleshoot this query by leaving off the LIMIT clause until you're sure you're getting the right information.
I have 3 tables, orders(orders_id, date, ...), products (product_id, product_name, product_price) and order_products (product_id, orders_id, product_id, products_quantity) and I need to group the products so that they are displayed by product and total quantity per product to make it easier for the eshop manager to know how many items per product have been ordered.
I'm having a little bot of trouble thinking of the correct sql syntax, I keep bumping into group by issues and i'd like some help.
This is what I've done so far
select *, op.products_quantity as pquant, count(*) as `count`
from orders o
left join orders_products op on o.orders_id = op.orders_id
left join products p on op.products_id = p.products_id
group by op.orders_products_id
order by op.products_id desc;
Looking at what you have, you're counting orders, not summing the quantity of the orders..
So
if you had
orders
monday 5 potatoes
tuesday 2 carrots
wednesday 3 potatoes
You wanted
potatoes 8
carrots 2
in which case you'd want to do
select sum(quantity),item from orders group by item
I didnt quite see what the differende between orders_products and orders was.
Maybe a bit of sample data would help?
Select p.Product_name,sum(prodcuts_quantity) as OrderedQuantity from
Order_products op join
Products p on p.Product_id = op.product_id
group by p.Product_name
If you need Total quantity and total orders per product than you can do that in following way
SELECT p.*,op.total_order,op.total_quantity FROM PRODUCT LEFT JOIN (SELECT COUNT(*) AS total_order, SUM(quantity) AS total_quantity FROM orders_products GROUP BY product_id) AS op ON p.id = op.product_id
This should work, use SUM, not COUNT:
SELECT
*,
SUM(op.products_quantity)
FROM
orders AS o
LEFT JOIN orders_products AS op ON o.orders_id = op.orders_id
LEFT JOIN products AS p ON op.products_id = p.products_id
GROUP BY p.products_id
ORDER BY p.products_id DESC