Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My table is as following

item | date       | q_in | q_out |
----------------------------------
 a   | 25-08-2016 |  100 |  50
 b   | 26-09-2016 |  100 |   0 
----- upto 
 b   | 10-09-2016 |    0 | 100


I need output like this :-

item | open_stock | inward | outward | balance|
-----|------------|--------|---------|--------|
 a   |    1500    |  10000 |  500    | 1100   |
 b   |     500    |   5000 | 1000    | 4500   | 


where

open_stock = balance of q_in - q_out upto 31-08-2016
inward = total q_in from 01-09-2016 to 10-09-2016
outward = total q_out from 01-09-2016 to 10-09-2016
balance = open_stock + inward - outward

What I have tried:

my query is as follows which needs your help

select 
    item,
    (select 
         (SUM(q_in) - SUM(q_out))
     from 
         sale_table
     where 
         date >= '2016-08-25' and date <= '2016-08-31') as open_stock,
    SUM(q_in) as inward,
    SUM(q_out) as outward ,
    (open_stock + inward - outward) as balance
from 
    sale_table
where 
    date >= '2016-09-01' and date <= '2016-09-10'
group by 
    item

i need values instead open_stock , inward and outward

thanks in advance ......
Posted
Updated 14-Sep-16 23:53pm
v2

Perhaps something like
SELECT a.item,
       a.open_stock,
       a.inward,
       a.outward,
       a.open_stock + a.inward - a.outward
FROM (select 
            item,
            (select 
                 (SUM(q_in) - SUM(q_out))
             from 
                 sale_table s2
             where 
                     s2.date >= '2016-08-25' 
                 and s2.date <= '2016-08-31'
                 and s2.item = sale_table.item) as open_stock,
            SUM(q_in) as inward,
            SUM(q_out) as outward 
        from 
            sale_table
        where 
            date >= '2016-09-01' and date <= '2016-09-10'
        group by 
            item) AS a
 
Share this answer
 
v2
Comments
Member 12741312 15-Sep-16 4:56am    
dear @Mike , thanks for your reply but it returns open_stock same for all items ... any rectification ???
Wendelius 15-Sep-16 6:10am    
It seems that your original query didn't have any correlation to the outer item. See the updated solution
Maciej Los 15-Sep-16 15:29pm    
5ed!
Wendelius 17-Sep-16 12:19pm    
Thanks :)
Seems you forgot to define proper relationship between query and subquery:

SELECT a.item, a.open_stock, a.inward, a.outward, a.open_stock + a.inward - a.outward as balance
FROM (select item, 
            (
             select (SUM(q_in) - SUM(q_out))
             from sale_table
             where date >= '2016-08-25' and date <= '2016-08-31' and item = first.item
            ) as open_stock,
            SUM(q_in) as inward,
            SUM(q_out) as outward 
        from sale_table As first
        where date >= '2016-09-01' and date <= '2016-09-10'
        group by first.item) AS a
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900