Triple Table Join with Subtraction - sql

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

Related

SQL -percent calculation

Make a report on the sales in 2015 of the products by categories (total value and
quantity sold). Also determine what% of the value of sales
for a given category represent the sales of each of the products in the category.
My query so far:
WITH sales AS
(SELECT t1.category_name
, t2.product_name
, (t3.unit_price*t3.quantity) Total_sales
, EXTRACT (YEAR FROM order_date) Year
FROM categories t1
INNER JOIN
products t2
ON t2.category_id=t1.category_id
INNER JOIN
order_details t3
ON t3.product_id=t2.product_id
INNER JOIN
orders t4
ON t4.order_id=t3.order_id
WHERE EXTRACT (YEAR FROM order_date) = '2015'
GROUP BY t1.category_name
, t2.product_name
, (t3.unit_price*t3.quantity)
, EXTRACT (YEAR FROM order_date)
ORDER BY 1
)
SELECT s.category_name
, s.product_name
, SUM (Total_sales)
FROM sales s
GROUP BY s.category_name
, s.product_name
ORDER BY 1
How to calculate %? Thank you
I think that you want window functions - if your database, that you did not specify, supports them:
SELECT
c.category_name
p.product_name
SUM(od.unit_price * od.quantity) as total_sales
1.0 * SUM(od.unit_price * od.quantity)
/ SUM(SUM(od.unit_price * od.quantity)) OVER(PARTITION BY c.category_id)
as category_sales_ratio
FROM categories c
INNER JOIN products t2 p ON p.category_id = c.ategory_id
INNER JOIN order_details od ON od.product_id = p.product_id
INNER JOIN orders o ON o.order_id = od.order_id
WHERE o.order_date >= '2015-01-01' AND o.order_date < '2016-01-01'
GROUP BY c.category_id, c.ategory_name, p.product_id, p.product_name
ORDER BY c.category_name, p.product_name
The window sum computes the total sales for the whole category, that you can divide the sales of the current product with.
Note that I changed your query in serveral ways:
meaningful table aliases make the query easier to write, read and maintain
filtering dates without transformation is much more efficient that using date functions
there is no need for a subquery
it is always a good idea to put the relevant primary keys in the GROUP BY clause (in case two different products or categories have the same name) - on the other hand, you also had additiona uneeded columns in that clause

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.

Select From 2 Selects

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é')

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 Grouping using left join

I have 2 tables with related data. One table is for products and the other price. In the price table one product may appear several times. How can I return the result by grouping.
Below is my Query but the output is not with Group
SELECT distinct
p.Product,
p.Qty,
MAX(pr.netprice)
FROM Products p
LEFT OUTER JOIN Price pr ON p.Product=pr.Product
WHERE p.brand=''
GROUP BY p.Product, p.Qty
You should probably leave the Qty out of the group by, like this:
SELECT p.Product,
MAX(pr.netprice)
FROM Products p
LEFT OUTER JOIN Price pr ON p.Product=pr.Product
WHERE p.brand=''
GROUP BY p.Product