Query between 2 tables - sql

I need to write a query between 2 tables that allow me know the total per SKUs.
Table 1: order_item
order_item_id(pk) order_id product_id qty_ordered total
Table 2:product
product_id SKU name price
I've tried the following:
from product
select SKU
Natural Join order_item

SELECT sum(qty_ordered), p.name, p.SKU
FROM product p
JOIN order_item o
ON p.product_id = o.product_id
group by
p.name, p.SKU
Useful link on aggregates and grouping: https://www.w3schools.com/sql/sql_groupby.asp

Related

Count between multiple tables

I have these 3 tables with the following structures:
Products
product_id | category_id | product_name
Customers
customer_id | customer_name
Orders
order_id | customer_id | product_id
I need to write a SQL query that will print the top customers regarding the number of categories they have bought products from (the best customer is the one that has bought from the most categories).
Can anyone show me how to do that?
I tried like this, but I get the following error "not a GROUP BY expression":
select
(select customer_name
from customers
where customers.customer_id = orders.customer_id) as name,
(select count(category_id)
from products
where products.product_id = orders.product_id)
from
orders
group by
customer_id
order by
count(customer_id) desc;
I managed to make the top regarding how many products the customers bought. The solution I used is:
select
(select customer_name from customers
where customers.customer_id = orders.customer_id) as name,
count(product_id) as "number of ordered products"
from
orders
group by
customer_id
order by
count(product_id) desc;
Nested queries for this? Sheesh...
Just join them already.
And then order by the unique categories
SELECT c.customer_name
, COUNT(DISTINCT p.category_id) AS TotalOrderedCategories
FROM Orders o
LEFT JOIN Customers c ON c.customer_id = o.customer_id
LEFT JOIN Products p ON p.product_id = o.product_id
GROUP BY c.customer_name
ORDER BY COUNT(DISTINCT p.category_id) DESC
Then depending on your RDBMS flavor, add a TOP or a LIMIT.

I've tried to retrieve the all the product and their total sales in the result

I have the Product table with the product name and ID then also have the receipt_product table with product ID and receipt ID.
And finally, have the receipt table with the amount and the receipt ID. So I've tried to retrieve all the products and their total sales in the result.
This is my query and I know that my query is wrong and need to do something to make it correct.
Tables
select
p.name, sum(total) as total sales
from
receipt_product rp
right join
product p on p.product_id = rp.product_id
inner join
receipt r on rp.receipt_id = r.receipt_id
group by
p.name
having
total_sales < 100
You have the price of individual product and the quantity sold . So why are you taking the receipt table in consideration.
Try something like this.
select sum(p.price * r.quantity_sold) as "Total Sales",p.name
from product p join receipt_product r
on p.product_id = r.product_id
group by p.name;

SQL Count with a Join

select product_name, product_order.Count(*)
From product Join product_order
ON product.product_id = product_order.product_id
where product.product_id = product_order.product_id;
I have been working on this query for an hour and can't seem to get it to work. All I need to do is take the product from one table match it to how many times it was ordered in another. Then display the product in one column and how many times it was ordered in the next.
This should be as simple as:
select product_name,
count(product_order.product_id)
From product left join product_order
on product.product_id = product_order.product_id
group by product_name
I used a left join and counted on product_order.product_id so products that have not been ordered will still display, with a count of zero.
This is how I would do it:
select p.product_name,
(select count(*) from product_order po where p.product_id = po.product_id) times_ordered
from product p
Alternatively, you could use a group by statement:
select p.product_name,
count(po.product_id)
from product p,
product_order po
where p.product_id = po.product_id(+)
group by p.product_name

SQL Join with SUM()

I have two Tables
Product Details(About Product)
Sale Order Details(What Price is sold, quantity of products sold per order).
I am trying to do Join on Table 1 and Table 2 which should give the all the product details and sum(Quantity), Sum(Price)
Problem Facing: There are some products in Table 1 which are never sold, and those rows are missing in the result set, but I want details for all the rows in Table 1 with rows of Products never purchased should be NULL or 'o'
Query I am Using:
select
P.*,
ISNULL((sum([Q.Quantity])),0),
ISNULL((sum([Q.Price])),0)
From Table1 P
Left Outer Join Table2 Q on P.Product_ID = Q.Product_ID
Please help me with any suggestions that would work for me
How about this one:
select
P.Product_ID,
ISNULL(sum([Q.Quantity]),0),
ISNULL(sum([Q.Price]),0)
From Table1 P
Left Outer Join Table2 Q
on P.Product_ID = Q.Product_ID
group by
P.Product_Id

SQL Server query to find the total price of all the products by order

I have a table Orders with (id,orderCode,productId,quantity,color,size)
where I can have entries like:
1,O20100812,163,2,BLUE,Medium
1,O20100812,163,3,BLUE,Larger
1,O20100812,145,4,RED,Large etc
1,O20100815,134,5,RED,Large etc
1,O20100815,143,2,BLACK,Large etc
1,O20100815,112,8,BLACK,Large etc
And another table Products with (id,name,price)
What I want is to find the total price of all the products in the order with orderCode 020100812. Should I DISTINCT select the order code and then SUM the quantity while JOINing the products table?
Why you need distinct?
Select SUM(o.Quantity * Price) TotalPrice
FROM Orders o JOIN Products p ON (o.ProductId = p.Id)
WHERE OrderCode = '020100812'
For all orders you can use the following query:
Select OrderCode, SUM(o.Quantity * Price) TotalPrice
FROM Orders o JOIN Products p ON (o.ProductId = p.Id)
Group by OrderCode
No, GROUP BY and then you can use SUM to aggregate across the group, e.g.
select O.id, O.ordercode, sum(P.price * O.quantity) as total
from orders O
join products P on P.id = O.productid
group by O.id, O.ordercode
which will show you the total price for each order code within each order - if you wanted all order codes across all orders you'd need
select O.ordercode, sum(P.price * O.quantity) as total
from orders O
join products P on P.id = O.productid
group by O.ordercode
i.e. don't group in the order ID.
(I'm guessing that O20100812 was just an example and you actually want this for all order codes?)