different count from 2 tables - mysql

I have 3 tables as
products TABLE
FIELDS: productid, catid,...
sales TABLE
FIELDS: trackid, productid,...
promotion TABLE
FIELDS: trackid, productid,...
Now I need something like
ProductID CatID TotalSales TotalPromotion
1 1 10 3
How can I achieve this?
Thanks for the help

Join the above three tables.

Try this:
SELECT a.productid,
a.catid,
a.totalsales,
b.totalpromotion
FROM (SELECT p.productid,
p.catid,
COUNT(s.productid) AS totalsales
FROM products p
LEFT JOIN sales s
ON p.productid = s.productid
GROUP BY p.productid,
p.catid) a
INNER JOIN (SELECT p.productid,
p.catid,
COUNT(s.productid) AS totalpromotion
FROM products p
LEFT JOIN promotion s
ON p.productid = s.productid
GROUP BY p.productid,
p.catid) b
ON a.productid = b.productid

Hope this help,
select a.productID, a.catID,
b.totalSales, c.Totalpromotion from
Product a inner join Sales b on
a.catID=b.CatID inner join Promotion c
on a.CatID=C.CatID

Related

MySQL: Ranking from multiple tables, sub queries?

This is a MySQL question. I have three tables with the following columns:
transactions (table): transact_id, customer_id, transact_amt, product_id,
products (table): product_id, product_cost, product_name, product_category
customers (table): customer_id, joined_at, last_login_at, state, name, email
I'd like a query that finds out the most popular item in every state and the state. One of the tricky parts is that some product_name have multiple product_id. Therefore I though joining the three tables that generate an output with two columns: state and product_name. Until here that worked fine doing this:
SELECT p.product_name, c.state
FROM products p
INNER JOIN transactions t
ON p.product_id = t.product_id
INNER JOIN customers c
ON c.customer_id = t.customer_id
This selects all the products, and the states from where the customer is. The problem is that I can't find the way to rank the mos popular product per state. I tried different group by, order by and using subqueries without success. I suspect I need to do subqueries, but I can't find the way to resolve it. The expected outcome should look like this:
most_popular_product | state
Bamboo | WA
Walnut | MO
Any help will be greatly appreciated.
Thank you!
You need a subquery that gets the count of transactions for each product in each state.
SELECT p.product_name, c.state, COUNT(*) AS count
FROM products p
INNER JOIN transactions t
ON p.product_id = t.product_id
INNER JOIN customers c
ON c.customer_id = t.customer_id
GROUP BY p.product_name, c.state
Then write another query that has this as a subquery, and gets the highest count for each state.
SELECT state, MAX(count) AS maxcount
FROM (
SELECT p.product_name, c.state, COUNT(*) AS count
FROM products p
INNER JOIN transactions t
ON p.product_id = t.product_id
INNER JOIN customers c
ON c.customer_id = t.customer_id
GROUP BY p.product_name, c.state
) AS t
GROUP BY state
Finally, join them together:
SELECT t1.product_name AS most_popular_product, t1.state
FROM (
SELECT p.product_name, c.state, COUNT(*) AS count
FROM products p
INNER JOIN transactions t
ON p.product_id = t.product_id
INNER JOIN customers c
ON c.customer_id = t.customer_id
GROUP BY p.product_name, c.state
) AS t1
JOIN (
SELECT state, MAX(count) AS maxcount
FROM (
SELECT p.product_name, c.state, COUNT(*) AS count
FROM products p
INNER JOIN transactions t
ON p.product_id = t.product_id
INNER JOIN customers c
ON c.customer_id = t.customer_id
GROUP BY p.product_name, c.state
) AS t
GROUP BY state
) AS t2 ON t1.state = t2.state AND t1.count = t2.maxcount
This is basically the same pattern as SQL select only rows with max value on a column, just using the first grouped query as the table you're trying to group.

Beginner sql student in need of assistance

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

Replacing id with name form another table in existing query

here's my query for getting the remaining stocks of each Product_Id. However, I'm not sure how to go about adding Product_Name from Product table. I just want to replace the i.Product_Id with p.Product_Name with p being the alias of Product. I know that the Product p should be inserted in the FROM but not sure how to connect it with the given query.
By the way, Product.Id = Inventory.Product_Id
SELECT
i.Product_Id,
i.Stocks,
s.Sales,
i.Stocks - s.Sales AS Remaining
FROM (SELECT product_id, COALESCE(SUM(quantity),0) AS Stocks FROM inventory GROUP BY product_id) i
LEFT JOIN (SELECT product_id, COALESCE(SUM(quantity),0) AS Sales FROM sales_detail GROUP BY product_id ) s
USING(product_id);
Thanks in advance guys!
UPDATE!!
The query below is my updated one, can someone check if I linked the product correctly with the existing JOIN USING, thanks!
SELECT
i.Product_Id,
p.Product_Name,
i.Stocks,
s.Sales,
i.Stocks - s.Sales AS Remaining
FROM (SELECT product_id, COALESCE(SUM(quantity),0) AS Stocks FROM inventory GROUP BY product_id) i
LEFT JOIN (SELECT product_id, COALESCE(SUM(quantity),0) AS Sales FROM sales_detail GROUP BY product_id ) s
USING(product_id)
JOIN Product p
ON i.Product_Id=p.Id;
You just need to JOIN with the Products table.
SELECT
p.Product_Name,
i.Stocks,
s.Sales,
i.Stocks - s.Sales AS Remaining
FROM (SELECT product_id, COALESCE(SUM(quantity),0) AS Stocks FROM inventory GROUP BY product_id) i
JOIN Products AS p USING (product_id)
LEFT JOIN (SELECT product_id, COALESCE(SUM(quantity),0) AS Sales FROM sales_detail GROUP BY product_id ) s
USING(product_id)

inner-join mysql x3

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

MySQL Query to fetch data from multiple tables

I have following tables
1) Products (productid, name, description, price)
2) Sales (salesid, productid, buyername, buyeremail, status)
3) ProductViews (viewid, productid)
Now, I need a query that can output as
ProductID ProductName Price TotalViews TotalSales
Help appreciated, thanks.
SELECT p.productid, p.name, p.price, COUNT(pv.viewid) AS totalviews, COUNT(s.salesid) AS totalsales
FROM Products p
LEFT JOIN Sales s ON s.productid = p.productid
LEFT JOIN ProductViews pv ON pv.productid = p.productid
GROUP BY p.productid, p.name, p.price
Extended group by for completeness sake but it could just be p.productid.
You can use subqueries to get the count of views and sales:
SELECT
a.productid,
a.name,
a.price,
(SELECT COUNT(b.viewid)
FROM ProductViews b
WHERE b.productid = a.productid) as TotalViews,
(SELECT COUNT(c.salesid)
FROM Sales c
WHERE c.productid = a.productid) as TotalSales
FROM
Products a
Select p.ProductID, p.ProductName, p.Price, s.c as TotalSales, v.c as TotalViews
FROM Products p
INNER JOIN (select productid, count(*) as c from Sales group by productid) s
ON s.productId = p.productid
INNER JOIN (select productid, count(*) as c from ProductViews group by productid) v
ON p.productId = v.productid
If you have a product that doesn't have a sale or a view you will need to left join instead
SELECT a.productid, a.name, a.price, (
SELECT COUNT( b.viewid )
FROM ProductViews b
WHERE b.productid = a.productid
) AS TotalViews, (
SELECT COUNT( c.salesid )
FROM Sales c
WHERE c.productid = a.productid
) AS TotalSales
FROM products a