0

1.products(产品表)

1.productid 

  001         
  002         
  003         

2.productname

  x
  y
  z

2.invoiceqty(发票数量表)

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

请告诉我如何编写这样的查询来计算库存余额

6.股票(查询)

1.productid 

  001
  002
  003

2.Stockqty

  40
  33
  50
4

2 回答 2

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; 
于 2012-06-22T07:38:39.103 回答
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
于 2012-06-22T08:09:02.747 回答