簡體   English   中英

在mysql視圖中計算每個產品的運行余額

[英]Calculate running balance per product in mysql view

我在mysql中有一個視圖( vwbalance ),用於檢查商店中我們產品的當前庫存余額,它已經與一個產品一起工作得非常好。 這是觀點

CREATE VIEW vwbalance AS
SELECT
  a.`id`      AS `Trans No`,
  a.`tdate` AS Siku,
  a.`section` AS `Section`,
  `g`.`p_name` AS `Product`,
  a.`cr`      AS `In`,
  a.`dr`      AS `Out`,
  SUM((o.`cr` - o.`dr`)) AS `balance`,
  a.`status`  AS `status`
FROM ((`trn_inventory` a
    LEFT JOIN `mst_product` `g`
      ON ((`g`.`p_id` = a.`p_id`)))
   JOIN `trn_inventory` o
     ON (((a.`tdate` > o.`tdate`)
           OR ((a.`tdate` = o.`tdate`)
               AND (a.`id` >= o.`id`)))))
WHERE (o.`status` = 'APPROVED')
GROUP BY a.`tdate` DESC,a.`id` DESC;

上面的視圖從兩個表trn_inventory獲取數據,其中我們存儲所有庫存交易(產品進入和產品出去)和mst_product ,我們存儲產品詳細信息。 我們創建此視圖的主要原因基本上是顯示運行平衡,因為表trn_inventory不存儲余額,下面是表定義

CREATE TABLE trn_inventory (
  id INT(25) NOT NULL AUTO_INCREMENT,
  tdate DATE NOT NULL,
   p_id INT(25) NOT NULL,
   dr INT(5) DEFAULT '0' COMMENT 'OUT',
  cr INT(5) DEFAULT '0' COMMENT 'IN',
  cost DOUBLE(13,2) NOT NULL DEFAULT '0.00',
  section VARCHAR(95) DEFAULT NULL,
  ref VARCHAR(95) DEFAULT NULL,
  trans_user VARCHAR(35) NOT NULL,
  `status` ENUM('PENDING','APPROVED','DISPATCHED','VOID') NOT NULL DEFAULT 'PENDING',
  approvedby VARCHAR(35) DEFAULT NULL,
  dispatchedby VARCHAR(35) DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=INNODB DEFAULT CHARSET=latin1;

這是我運行SELECT * FROM vwbalance;時的輸出SELECT * FROM vwbalance;

Trans  Siku      Section   Product         In   Out  Bal  Status  
-------------------------------------------------------------------
8   2014-02-05  "Store"   "Treated SEEDS"   0   10   68  "APPROVED"
7   2014-02-05  "Store"   "Treated SEEDS"  50    0   78  "APPROVED"
5   2014-02-04  "Store"   "Dry Seeds"      40    0   28  "APPROVED"
3   2014-01-16  "Store"   "Dry Seeds"       0    2  -12  "APPROVED"
4   2014-01-15  "Store"   "Dry Seeds"       0   15  -10  "APPROVED"
2   2014-01-15  "Store"   "Dry Seeds"      10    0    5      "VOID"
1   2014-01-15  "store"   "Dry Seeds"      12    0    5  "APPROVED"
6   2014-01-14  "Store"   "Dry Seeds"       0    7   -7  "APPROVED"

我希望它顯示每個產品的余額:

Trans  Siku      Section   Product         In   Out  Bal  Status  
-------------------------------------------------------------------
8   2014-02-05  "Store"   "Treated SEEDS"   0   10   40  "APPROVED"
7   2014-02-05  "Store"   "Treated SEEDS"  50    0   50  "APPROVED"
5   2014-02-04  "Store"   "Dry Seeds"      40    0   28  "APPROVED"
3   2014-01-16  "Store"   "Dry Seeds"       0    2  -12  "APPROVED"
4   2014-01-15  "Store"   "Dry Seeds"       0   15  -10  "APPROVED"
2   2014-01-15  "Store"   "Dry Seeds"      10    0    5      "VOID"
1   2014-01-15  "store"   "Dry Seeds"      12    0    5  "APPROVED"
6   2014-01-14  "Store"   "Dry Seeds"       0    7   -7  "APPROVED"

我修改了分組,

...
...
 WHERE (o.`status` = 'APPROVED')
    GROUP BY a.`tdate` DESC,a.`id` DESC,o.p_id;

但它下面的第二個產品返回兩行是輸出

Trans  Siku      Section   Product         In   Out  Bal  Status  
-------------------------------------------------------------------
 8  2014-02-05  "Store"   "Treated SEEDS"   0   10   28  "APPROVED"
 8  2014-02-05  "Store"   "Treated SEEDS"   0   10   40  "APPROVED"
 7  2014-02-05  "Store"   "Treated SEEDS"  50    0   28  "APPROVED"
 7  2014-02-05  "Store"   "Treated SEEDS"  50    0   50  "APPROVED"
 5  2014-02-04  "Store"   "Dry Seeds"      40    0   28  "APPROVED"
 3  2014-01-16  "Store"   "Dry Seeds"       0    2  -12  "APPROVED"
 4  2014-01-15  "Store"   "Dry Seeds"       0   15  -10  "APPROVED"
 2  2014-01-15  "Store"   "Dry Seeds"      10    0    5      "VOID"
 1  2014-01-15  "store"   "Dry Seeds"      12    0    5  "APPROVED"
 6  2014-01-14  "Store"   "Dry Seeds"       0    7   -7  "APPROVED"

我哪里錯了?

我創建了一個SQLFiddle ,您可以在其中使用示例數據獲取此模式,您可以在其中測試查詢

您在創建視圖中未匹配兩個表的產品ID。 所以,你需要設置如下條件。

 WHERE (o.status = 'APPROVED' and o.p_id = a.p_id)
GROUP BY a.`tdate` DESC,a.`id` DESC,o.p_id;

FIDDLE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM