Select From 2 Selects - sql

I need some help I have 3 tables
http://i.stack.imgur.com/TB3MX.jpg
My code:
select nazev_produkt,
SUM(mnozstvi_objednavka_dod)'bylo na sklade'
from Produkty p join Objednavky_dodavatele od on
p.id_produkt=od.id_produkt
where stavzbozi_objednavka_dod=('na skladě')
group by nazev_produkt
select nazev_produkt,SUM(mnozstvi_objednavka_zak)koupili
from Objednavky_zakaznici oz join Produkty p
on oz.id_produkt=p.id_produkt
where stav_objednavka_zak=('dodané')
group by nazev_produkt
I want to create select code from both of this selected which will consist of nazev_produkt, 'bylo na sklade - koupili' and group by nazev_produkt
basically
bylo na sklade = was in stock
koupili = purchased.
I need to deduct the purchased - was in stock and show nazev_produkt (Product Name), na sklade (In Stock)
Help is much appreciated!

I can't read your language, so making a few guesses here... :-) The table joining, I think, answers your question in any case.
select
p.product_name,
--isnull(i.quantity,0) as was_in_stock,
--isnull(s.quantity,0) as sold,
isnull(i.quantity,0) - isnull(s.quantity,0) as [currently_in_stock]
from
products p
LEFT JOIN (select product_id, sum(quantity) as quantity from inventory group by product_id) i
ON p.product_id = i.product_id
LEFT JOIN (select product_id, sum(quantity) as quantity from sales group by product_id) s
ON p.product_id = s.product_id
group by
product_name

Add a third column with the typization of your source.
Try this:
select nazev_produkt,
isnull(
(select SUM(mnozstvi_objednavka_dod)
from Objednavky_dodavatele od
where p.id_produkt=od.id_produkt),
0) -
isnull(
(select SUM(mnozstvi_objednavka_zak)
from Objednavky_zakaznici oz
where oz.id_produkt=p.id_produkt),
0)
from Produkty p
where stavzbozi_objednavka_dod=('na sklade')
or stav_objednavka_zak=('dodané')

Related

How to find the earnings from the most sold product with sql?

I have two tables:
I need to find the product name that was sold the most and the earnings from that.
The code I wrote:
SELECT *
FROM Products
WHERE ProductId = (SELECT ProductId
FROM
(SELECT
ProductId,
SUM(Quantity) AS total_order,
MAX(SUM(Quantity)) OVER () AS maxSm
FROM
Orders
GROUP BY
ProductId)
WHERE
total_order = maxSm)
But with this I just find the product name that was sold the most. Can you tell me please how can I find the earnings only from this product?
select top 1
a.name,
(b.total * a.price) as revenue
from
products a
left join (select productid, sum(quantity) as total group by productid) b
on a.productid = b.productid
order by
b.total desc
You need to join the result of your derived table to your Products table.
Without actual sample data I am unable to test, however the following should be what you need, or at least very close:
select p.Name, o.total_order, o.total_order * p.Price as TotalValue
from (
select * from (
select ProductId,
Sum(Quantity) as total_order,
Max(Sum(Quantity)) over () as maxSm
from Orders
group byProductId
)t
where total_order = maxSm
)o join Products p on p.ProductId=o.ProductId

How to create a function that totals an order with several items?

I need to create a function which will return the total of an order. I've been given three tables with the following variables
Table 1 - Order
Order_ID
Date_Placed
Date_Fulfilled
Table 2 - Order Product
Order_ID
Product_ID
Product_Quantity
Table 3 - Product
Product_ID
Price
I'm struggling to put together a coherent function. Any help would be greatly appreciated.
I've already attempted to set up the function with joins between both tables, but am unable to figure out where I should be putting my equation.
BEGIN
SELECT order.order_id, SUM(product.price * order_item.quantity)
FROM `order`
JOIN `order_item` ON order.order_id = order_product.order_id
JOIN `product` ON order_product.product_id = product.product_id;
END $$
You might be surprised, but the orders table is not needed for this query. You can just aggregate off the other two tables:
SELECT oi.order_id, SUM(p.price * oi.quantity)
FROM order_item oi JOIN
product p
ON po.product_id = p.product_id
GROUP BY oi.order_id;
You'll need to take your select statement, and group it by your order.order_id. That way you'll have one row per order, with the sum total of that order.
SELECT order.order_id, SUM(product.price * order_item.quantity) as total_price
FROM `order`
JOIN `order_item` ON order.order_id = order_product.order_id
JOIN `product` ON order_product.product_id = product.product_id
GROUP BY order.order_id
this will work:
SELECT order.order_id, SUM(product.price * order_item.quantity)
FROM order o,
JOIN order_item oi,
JOIN product p where
o.order_id = oi.order_id and
oi.product_id = p.product_id
group by order_product.product_id = product.product_id;

Triple Table Join with Subtraction

I have 3 tables (production, sales, wastage)
All 3 tables have the columns: Product and Quantity
I need a query statement that will join all 3 tables in the following manner:production - sales - wastage Grouped by Product
Thanks in advance for the tips and tricks :)
Combine tables using left join, group by product and get total quantity by substracting values assuming that there can be no record for a given product within sales or wastage tables and then count them as 0.
select p.product, sum(p.quantity - coalesce(s.quantity,0) - coalesce(w.quantity,0)) as qty
from production p
left join sales s on p.product = s.product
left join wastage w on p.product = w.product
group by p.product
You can group each table individually and then [left] join them:
SELECT p.product, p_quantity - COALESCE(s_quantity, 0) - COALESCE(w_quantity, 0)
FROM (SELECT product, SUM(quantity) AS p_quantity
FROM production
GROUP BY product) p
LEFT JOIN (SELECT product, SUM(quantity) AS s_quantity
FROM sales
GROUP BY product) s ON p.product = s.product
LEFT JOIN (SELECT product, SUM(quantity) AS w_quantity
FROM wastage
GROUP BY product) w ON p.product = w.product

Sql Table Left outer join result Group By

Product1
Purchase quantity from 3 invoices are 50+100+50 = 200 and
Sale quantity from 1 invoice is 10
I am using below code for getting the result as
Total Purchase - Total Sale = Closing Qty
200 - 10 = 290
but I am getting the wrong result as shown in the attached image:
Please guide me to correct my code
SELECT
P.PRODUCT as PRODUCTNAME,
P.QUANTITY AS PURCHASE,
ISNULL(S.QUANTITY, 0) AS SALE,
ISNULL(P.QUANTITY, 0) - ISNULL(s.QUANTITY, 0) AS CLOSINGQTY
FROM
[PurchaseData] P
LEFT OUTER JOIN
[DeliveryData1] S ON P.Product = s.PRODUCT
When working with aggregates from different tables, don't join the tables and aggregate then, but aggregate first and join the aggregates:
select
p.product as productname,
p.sum_quantity as purchase,
coalesce(s.sum_quantity, 0) as sale,
p.sum_quantity - coalesce(s.sum_quantity, 0) as closingqty
from
(
select product, sum(quantity) as sum_quantity
from purchasedata
group by product
) p
left join
(
select product, sum(quantity) as sum_quantity
from deliverydata1
group by product
) s on s.product = p.product;
I've replaced ifnull with coalesce, so the query is fully standard SQL compliant.
If you want the final result you should use sum and group by
SELECT
P.PRODUCT as PRODUCTNAME,
sum(P.QUANTITY AS PURCHASE),
sum(isnull(S.QUANTITY,0)) AS SALE,
sum(isnull(P.QUANTITY,0))-sum(isnull(s.QUANTITY,0)) AS CLOSINGQTY
FROM [PurchaseData] P LEFT OUTER JOIN [DeliveryData1] S ON P.Product = s.PRODUCT
GROUP BY P.PRODUCT
If I understand correctly, you have a problem because you have multiple purchase records for a given product. If that is the case, then just aggregate before the JOIN:
SELECT p.*, COALESCE(S.QUANTITY, 0) AS SALE,
COALESCE(P.QUANTITY, 0)-COALESCE(s.QUANTITY, 0) AS CLOSINGQTY
FROM (SELECT p.product, p.quantity
FROM PurchaseData P
GROUP BY p.product
) p LEFT OUTER JOIN
DeliveryData1 S
ON P.product = s.producct;
Actually, I'm unclear which of the two tables has the duplicates -- or even if both do. So, you might need to do something similar for S.

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