tags:

views:

43

answers:

2

I've two tables as purchase_details and invoice_details

and i want to store the inventory/stock of each product from the data of these two tables.

structure of purchase_details.

'pid', 'int(10)'
'product_id', 'int(10)' 
'quantity', 'float(8,2)'
'amount', 'float(12,2)'
'expiry_date', 'date'

structure of invoice_details.

'invoice_id', 'int(10) unsigned'
'product_id', 'int(10) unsigned'
'quantity', 'float(10,2)'
'price', 'float(12,2)'

i want to calculate the total quantity of remaining stock (quantity of sum of products from purchase_details) - (quantity of sum of products from invoice_details)

Product_id is would be same for the two tables.

Product table consists of product data, and the structure is

'id', 'int(10) unsigned'
'cid', 'int(10) unsigned'
'name', 'varchar(90)'
'selling_price', 'float(10,2)'
'mrp', 'float(10,2)'
'reorder_level', 'bigint(20) unsigned'

the invoice_details may or may not have entries for every product_id.

how can i proceed?

+1  A: 

This one is my new answer, already tested on sqlserver. I am not sure about mysql

select pro.id, SUM(pro.quantity - inv.quantity)
from (select sum(p.quantity) as quantity, p.id as id from product p group by p.id) as pro, 
     (select sum(i.quantity) as quantity, i.id as id from invoice i group by i.id) as inv
where inv.id = pro.id
group by pro.id;
vodkhang
its not working.. i've multiple products in product table. And product_id will be repeated in both purchase_details and invoice_details table. And i want to calc stock for each and every product
kvijayhari
uhm, that means, you want to list the stock (the difference) for every product id?
vodkhang
exactly thats what i want..
kvijayhari
i tried the following query but the results r not great. select a.pid, a.amount - b.amount from (select product_id as pid, COALESCE(sum(quantity),0) amount from purchase_details group by product_id) a, (select product_id as pid, COALESCE(sum(quantity), 0) amount from invoice_details group by product_id) b group by a.pid;that returns7, 124.00 8, 8.00but it should be 7,124.00 and 8,20 for 7 the value are 136 in purchase and sales 12 so its correct as 124 but for 8 the purchase is 20 and sales has no values but even it subtracts the 12 and returns as 8.. Can u help from here?
kvijayhari
My new answer look similar to this except some minor difference. Can you edit the question so that it is well formatted. It is tough for me to read anyway
vodkhang
The following is the query i tested last and urs is also not working well . may be i would have given the wrong table names for the query.. find the names of the table i have used in the below queryselect a.pid, a.amount - b.amount from (select product_id as pid, COALESCE(sum(quantity),0) amount from purchase_details group by product_id) a, (select product_id as pid, COALESCE(sum(quantity), 0) amount from invoice_details group by product_id) b group by a.pid;
kvijayhari
A: 
select  ( sum(purchase_details.quantity) - sum(invoice_details.quantity)) as stock  
    from products 
    left outer join purchase_details 
    on products.product_id == purchase_details.product_id
    left outer join invoice_details  
    on  products.product_id  =  invoice_details.product_id 
    group by products.product_id

this will give you the stock for each product and you can add your where statement if you need certain products

select  ( sum(purchase_details.quantity) - sum(invoice_details.quantity)) as stock  
    from products 
    left outer join purchase_details 
    on products.product_id == purchase_details.product_id
    left outer join invoice_details  
    on  products.product_id  =  invoice_details.product_id 
    where products.product_id = 137
    group by products.product_id
Amgad Fahmi
It returns wrong value.. I've 136 for product id 7 and 20 for product id 8 in purchase, 12 for product id 7 in invoice , so total should be 124 for productid 7 and 20 for product id 8 .. But its returing 224 and null
kvijayhari
because you need to call the product table first , i will modify the statement take a look
Amgad Fahmi
no its still not workingMy product table has unique ids but the product_id may be repeated in both purchase_details and invoice_details.
kvijayhari
yeah i know if the products table have unique id then this should work the only thing u need to add check if the value is null make it = 0
Amgad Fahmi
any way i can add check to make it 0 but the total is coming as wrong for other, i dont know why it happens? the total should be 124 but it is returning as 224
kvijayhari