Search code examples
sqlms-accessms-access-2010

The SQL query of detail in and details out with a four column primary key to generate stock does not match the results in the MS Access database


Only results for in column QTY_STOCK are not correct. Maybe there is something wrong with the SQL code that I made. So if there is an answer then I don't want to use the function in MS Access because I want to use the SQL for VB.Net. Please guide me.

Thanks

SELECT  
    t1.CodeProduct, t1.Barcode, t1.Colorcode, t1.Size, 
    t1.ColorName,
    (t1.Qty - t2.Qty) AS QTY_STOCK, 
    t1.Qty AS QTY_STOCKIN, t2.Qty AS QTY_STOCKOUT
FROM
    StocksinDetail AS t1
LEFT JOIN 
    StocksoutDetail AS t2 ON (t1.Size = t2.Size)
                          AND (t1.Colorcode = t2.Colorcode)
                          AND (t1.Barcode = t2.Barcode)
                          AND (t1.CodeProduct = t2.CodeProduct);

Results from this SQL:

RESULTFROMSQL

This would be my desired result:

Tables-Stocks


Solution

  • Here's a link! According to the link above, I got the solution

    SELECT  
        t1.CodeProduct, t1.Barcode, t1.Colorcode, t1.Size, 
        t1.ColorName,
        IIf(IsNull(t1.Qty),'0',t1.Qty)-IIf(IsNull(t2.Qty),'0',t2.Qty) AS QTY_STOCK,
        t1.Qty AS QTY_STOCKIN, t2.Qty AS QTY_STOCKOUT
    FROM
        StocksinDetail AS t1
    LEFT JOIN 
        StocksoutDetail AS t2 ON (t1.Size = t2.Size)
                              AND (t1.Colorcode = t2.Colorcode)
                              AND (t1.Barcode = t2.Barcode)
                              AND (t1.CodeProduct = t2.CodeProduct);