Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtract two columns from two tables with Group By

I have 4 tables.

CREATE TABLE Branch(
  ID INT,
  Name VARCHAR(50)
);

INSERT INTO Branch VALUES 
(1,'A'), (2,'B');

CREATE TABLE Product(
  ID INT,
  Name VARCHAR(50)
);

INSERT INTO Product VALUES 
(1,'X'), (2,'Y');

CREATE TABLE StockIn(
  ID INT,
  ProductId INT,
  Quantity INT,
  BranchId INT
);

INSERT INTO StockIn VALUES 
(1,1,10,1),
(2,1,20,1),
(3,1,50,2),
(4,1,10,2);

CREATE TABLE StockOut(
  ID INT,
  ProductId INT,
  Quantity INT,
  BranchId INT
);

INSERT INTO StockOut VALUES 
(1,1,5,1),
(2,1,21,1),
(3,1,45,2),
(4,1,5,2);

Now i want to calculate stock from these (StockIn-StockOut).

by using below query i got stockin and stockout from stock tables by grouping thier branches.

StockIn

select BranchId, ifnull(sum(Quantity),0) Quantity from stockin where productid=1 group by BranchId;

enter image description here

StockOut

select BranchId, ifnull(sum(Quantity),0) Quantity from stockout where productid=1 group by BranchId;

enter image description here

and i want to show the result like this

enter image description here

like image 770
Roshan Avatar asked Sep 24 '18 07:09

Roshan


People also ask

How do I subtract two columns from different tables in SQL?

Basic Syntax:SELECT column1 , column2 , ... columnN FROM table_name WHERE condition MINUS SELECT column1 , column2 , ... columnN FROM table_name WHERE condition; columnN: column1, column2.. are the name of columns of the table.

Can we use group by for two columns?

Yes, it is possible to use MySQL GROUP BY clause with multiple columns just as we can use MySQL DISTINCT clause.

Can we use group by for two tables?

Unfortunately, groupby function does not allow you to use multiple table columns, you can only use one table which defined in the function table parameter.


2 Answers

  • For each individual Select query results, get an additional field, namely, factor. Its value will be +1 for stock in, and -1 for stockout.
  • Combine the results of individual select queries using Union All, and utilize the resultset as a Derived Table.
  • Now, simply do a Sum again, multiplying with the factor, on a grouping of BranchId.

Try the following query:

SELECT derived_t.BranchId, 
       SUM(derived_t.factor * derived_t.quantity) AS Quantity 
FROM 
(
 select BranchId, 
        ifnull(sum(Quantity),0) as quantity, 
        1 as factor 
 from stockin 
 where productid=1 
 group by BranchId

 UNION ALL 

 select BranchId, 
        ifnull(sum(Quantity),0) Quantity, 
        -1 as factor
 from stockout 
 where productid=1 
 group by BranchId
) AS derived_t 

GROUP BY derived_t.BranchId
like image 80
Madhur Bhaiya Avatar answered Sep 21 '22 10:09

Madhur Bhaiya


You could use a join between the two query

select a.branchId, a.quantity - ifnull(b.quantity,0) result
from (
    select BranchId, ifnull(sum(Quantity),0) Quantity 
    from stockin 
    where productid=1 
    group by BranchId
) a left join (
   Select BranchId, ifnull(sum(Quantity),0) Quantity 
   from stockout
   where productid=1 
   group by BranchId
) b on a.BranchId = b.BranchId
like image 32
ScaisEdge Avatar answered Sep 21 '22 10:09

ScaisEdge