SQL Join 3 tables group sum - sql

Products with Id-Prod , desc, date fields
ENTRIES with Id-Prod Quantity, Amount.
Sorties with Id-Prod Quantity, Amount.
I want to make a query which join the 3 tables on Id-Prod and grouping it and sum the quantity and Amount for each products them calculate the stock which is equal to sum quantityIn -Sum QUantityOut
Thank you

To join and sum with 1-N looks like this -- we need to do a sub query to get the sums before the left join and a coalesce to set nulls to 0.
SELECT P.ID-Prod,
COALESCE(E.s,0) AS IN-Q,
COALESCE(S.s,0) AS OUT-Q,
COALESCE(E.s,0) - COALESCE(S.s,0) AS Stock
FROM Products P
LEFT JOIN (SELECT ID-Prod, SUM(Quantity) AS s
FROM ENTRIES GROUP BY ID-Prod) E ON P.ID-Prod = E.ID-Prod
LEFT JOIN (SELECT ID-Prod, SUM(Quantity) AS s
FROM Sorties GROUP BY ID-Prod) S ON P.ID-Prod = S.ID-Prod
GROUP BY P.ID-Prod

Related

SQL: Order by count return only the first row

I have two tables products and product_views.
I want to write a query that returns the products with the largest number of views.
products table has id and name columns. product_views has columns id, customer_id, product_id. The counting is based on the product_id column.
Here is my query:
SELECT *
FROM products
LEFT JOIN product_views ON products.id = product_views.product_id
ORDER BY COUNT(product_views.product_id) DESC;
The problem is it returns only the first product. I think the issue is that initially the product_views table is all empty (all values are null).
How to solve this issue?
Based on your criteria, the following should work -
SELECT
p.id,
p.name,
count(v.product_id) as views
FROM products p
LEFT JOIN product_views v
ON p.id = v.product_id
GROUP BY 1,2
ORDER BY views DESC;
The query ignores customer_id, and counts product_ids based on the id/name groupings.
Calculates count in separate script:
SELECT products.id, products.name, pv.count FROM products
JOIN (SELECT product_id, COUNT(*) as count FROM product_views GROUP BY product_id)
pv ON (products.id = product_id )
ORDER BY pv.count DESC;
Example: SQL Fiddle

Select the name of a buyer with the highest sum of orders

I have two tables:
Buyers:
Orders:
I want to get the name of a buyer with the highest amount of orders (in this case it would be buyer C with id 3).
I wrote the following query:
select top 1 b.Name, sum (o.Amount) as amt from Buyers b
join Orders o on o.BuyerId = b.Id
group by Name
order by amt desc
It gives me Name and amt in resulting row.
How to exclude amt from the result and display only Name of the buyer?
remove sum (o.Amount) as amt from select and add sum (o.Amount) desc in order by
select top 1 b.Name from Buyers b
join Orders o on o.BuyerId = b.Id
group by Name
order by sum (o.Amount) desc

SQL Sumation from Different Tables

I would like some help on this matter.
I Have three tables.
Products Table,
Invoice Table,
Cash Table
Products Table has 2 Columns as follows (PID,BRAND)
Invoice Table has 2 columns as follows (PID,TOTALSALES)
Cash Table has 2 columns as follows (PID,TOTALSALES)
The PID's could have many different BRANDS.
for eg:
PID BRAND
1 TEST1
2 TEST2
3 TEST3
All the three tables are linked by the PID column.
i have many rows in the invoice and cash tables.
My problem is to Group the SUMMED values of the TOTALSALES in the INVOICE and CASH tables BRAND wise.
I tried using left joins but the summation value always increases when there are more than 2 table in the join.
Any help will be highly appreciated.
You can use union all and then aggregate the results from the two tables together:
select p.brand, sum(invoice_sales) as invoice_sales, sum(cash_sales) as cash_sales
from ((select pid, totalsales as invoice_sales, 0 as cash_sales
from invoice i
) union all
(select pid, 0, totalsales
from cash c
)
) ic join
product p
on ic.pid = p.id
group by p.brand;
summarise the data in the Invoice and Cash tables separately
SELECT
pr.BRAND, (ISNULL(Inv.TOTALSALES, 0.00) + ISNULL(Csh.TOTALSALES, 0.00)) TotalSales
FROM Products pr
LEFT JOIN (
SELECT
PID, SUM(TOTALSALES) TOTALSALES
FROM Invoice GROUP BY PID
) Inv ON Inv.PID = pr.PID
LEFT JOIN (
SELECT
PID, SUM(TOTALSALES) TOTALSALES
FROM Cash GROUP BY PID
) Csh ON Csh.PID = pr.PID
ORDER BY pr.BRAND
You need two separate sums for the results of joining the tables by PID values and you will need to group the records by BRAND
select sum(Invoice.TOTALSALES) as invoice_sales, sum(Cash.TOTALSALES) as cash_sales
from Products
join Invoice
on Products.PID = Invoice.PID
join Cash
on Products.PID = Cash.PID
group by Invoice.PID, Products.BRAND;
If you do not need to group by products, then just omit Invoice.PID from the group by.
another way to do it is by using subquerys
select p.*,
(select sum(i.TOTALSALES) from Invoice i where i.PID = p.PID) as TotalSalesInvoice,
(select sum(c.TOTALSALES) from Cash c where c.PID = p.PID) as TotalSalesCash
from Products p

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 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