在mysql视图中计算每个产品的运行余额

时间:2014-02-05 07:32:23

标签: mysql sql select view group-by

我在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;时的输出:

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,您可以在其中获取包含示例数据的模式,您可以在其中测试查询

1 个答案:

答案 0 :(得分:3)

您在创建视图中未匹配两个表的产品ID。 因此,您需要将您的条件设置如下。

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

见工作FIDDLE