2012-06-22 55 views
0

1,產品(產品表)計算與多個在MySQL加入

1.productid 

    001   
    002   
    003   

2.productname 

    x 
    y 
    z 

2.invoiceqty(InvoiceQuantity表)

1.productid 

    001  
    001  
    002  
    002 

2.invqty 

    10 
    20 
    10 
    05 

3.grnqty(GRN數量表)

1.productid 

    001   
    002   
    003   

2.grqty 

    50 
    50 
    50 

4.returninqty(返回在數量表)

1.productid 

    002 
    002 

2.retinqty 

    02 
    03 

5.returnoutqty(返回退出數量表)

1.productid 

    001 
    001 

2.retoutqty 

    01 
    01 

我需要執行此查詢,查看庫存餘額 stockbalance計算這樣

(grnqty + returninqty) - (+ invoiceqty returnoutqty)= StockBalance

請告訴我怎麼寫,計算庫存餘額這樣 我有開發Java中的庫存控制系統的查詢/ MySQL的,請幫助我在此查詢

6.Stocks(查詢)

1.productid 

    001 
    002 
    003 

2.Stockqty 

    40 
    33 
    50 
+0

你被困在未返回預期結果,還是你只是尋找,會做你的描述語句的查詢? –

回答

0

該查詢是根據您的公式,但你顯示沒有生產,因爲我覺得你的公式可能有任何問題

SELECT 
    p.productid, 

    (ifnull(g.g_sum,0) + ifnull(rq.rq_sum,0))-(ifnull(i.i_sum,0) + ifnull(ro.ro_sum,0)) as StockBalance, 

    ifnull(g.g_sum,0) as GRN_QUANTITY, 
ifnull(rq.rq_sum,0) as RETURN_IN_QUANTITY, 
ifnull(i.i_sum,0) as INVENTORY_QUANTITY, 
ifnull(ro.ro_sum,0) as RETURN_OUT_QUANTITY 
from products as p 
    left join (select 
      productid, 
      sum(grnqty) as g_sum 
     from grnqty group by productid) as g 
    on g.productid = p.productid 
    left join (select 
      productid, 
      sum(retinqty) as rq_sum 
     from returninqty group by productid) as rq 
    on rq.productid = p.productid 
    left join (select 
      productid, 
      sum(invqty) as i_sum 
     from invoiceqty group by productid) as i 
    on i.productid = p.productid 
    left join (select 
      productid, 
      sum(retoutqty) as ro_sum 
     from returnoutqty group by productid) as ro 
    on ro.productid = p.productid 
結果
+0

非常感謝raheel shan先生!它的工作..非常感謝你,我們需要你的幫助更多..再次感謝.. – Rajive

+0

據我所知mysql可用 –

1

嘗試計算量使用子查詢每個表。它不適用於LEFT JOIN,因爲我們會在桌子上放置笛卡爾產品。

SELECT 
    p.productid, 
(
    SELECT COALESCE(SUM(grnqty), 0) 
    FROM grnqty g 
    WHERE g.productid = p.productid 
) 
+ 
(
    SELECT COALESCE(SUM(returninqty), 0) 
    FROM returninqty ri 
    WHERE ri.productid = p.productid 
)  
- 
(
    SELECT COALESCE(SUM(invoiceqty), 0) 
    FROM invoiceqty i 
    WHERE i.productid = p.productid 
) 
- 
(
    SELECT COALESCE(SUM(returnoutqty), 0) 
    FROM returnoutqty ro 
    WHERE ro.productid = p.productid 
) as stock 

FROM products p 

此外,您可以創建一個函數來計算股票。您可以輕鬆地將其用於其他查詢。

DELIMITER $$ 

CREATE FUNCTION `get_product_stock` (IN p_id_product INT) RETURNS INT 
BEGIN 

DECLARE stock INT; 

SET stock = 
    (
     SELECT COALESCE(SUM(grnqty), 0) 
     FROM grnqty 
     WHERE productid = p_id_product 
    ) 
    + 
    (
     SELECT COALESCE(SUM(returninqty), 0) 
     FROM returninqty 
     WHERE productid = p_id_product 
    ) 
    - 
    (
     SELECT COALESCE(SUM(invoiceqty), 0) 
     FROM invoiceqty 
     WHERE productid = p_id_product 
    ) 
    - 
    (
     SELECT COALESCE(SUM(returnoutqty), 0) 
     FROM returnoutqty 
     WHERE productid = p_id_product 
    ); 

RETURN stock; 

END $$ 

DELIMITER ; 

您可以使用這樣的功能:

// get the stock for product with id 1 
SELECT get_product_stock(1); 

// get all the products with their stock 
SELECT productid, name, get_product_stock(productid) as stock FROM products; 
+0

謝謝,我試過了,但沒有顯示實際價值 – Rajive

+0

您是對的,我相信這些款項不起作用,因爲我們是按id產品分組的,我們在所有這些表中都有笛卡爾產品。 @Rajive不客氣,我會更新我的答案 –

+0

感謝mazzucci先生 – Rajive