获得库存平衡

时间:2012-04-09 19:41:35

标签: mysql join sum

我有3张表如下:

products:[product_id,product_name]
stock:[product_id,date,qty_received]
sales:[product_id,date,qty_sold]

我试图通过减去qty_received - qty_sold来显示所有产品的余额。 我的疑问是:

SELECT DISTINCT p.product_id, p.product_name,
IFNULL(sum(qty_received),0)-IFNULL(sum(qty_sold),0) as balance FROM products p
LEFT JOIN stock st ON (p.product_id=st.product_id)
LEFT JOIN sales sa ON (p.product_id=sa.product_id)
GROUP BY p.product_id

通过上述查询,我​​得不到正确的余额。 能帮忙吗?

其他信息: 以下是我的表格的简化结构 产品表:

CREATE TABLE IF NOT EXISTS `products` (
  `product_id` varchar(15) NOT NULL,
  `product_name` varchar(20) NOT NULL,
  UNIQUE KEY `product_id` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

- 转储表products

的数据
INSERT INTO `products` (`product_id`, `product_name`) VALUES
  ('111', 'Product One'),
  ('222', 'Product Two'),
  ('333', 'Product Three'),
  ('444', 'Product Four');

股票表:

CREATE TABLE IF NOT EXISTS `stock` (
  `stock_id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) NOT NULL,
  `qty_received` int(11) NOT NULL,
  PRIMARY KEY (`stock_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=112 ;

- 转储表stock

的数据
INSERT INTO `stock` (`stock_id`, `product_id`, `qty_received`) VALUES
  (1, 111, 5),
  (2, 222, 10),
  (3, 333, 4),
  (4, 444, 6),
  (5, 111, 2),
  (6, 222, 7),
  (7, 111, 3),
  (8, 111, 3);

销售表

CREATE TABLE IF NOT EXISTS `sales` (
  `sales_id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) NOT NULL,
  `qty_sold` int(11) NOT NULL,
  PRIMARY KEY (`sales_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

- 转储表sales

的数据
INSERT INTO `sales` (`sales_id`, `product_id`, `qty_sold`) VALUES
  (1, 111, -1),
  (2, 222, -3),
  (3, 333, -2),
  (4, 111, -3),
  (5, 222, -1),
  (6, 222, -4);

我得到的结果是:

+------------+--------------+---------+
| product_id | product_name | balance |
+------------+--------------+---------+
| 111        | Product One  | 10      |
| 222        | Product Two  | 35      |
| 333        | Product Three| 2       |
| 444        | Product Four | 6       |
+------------+--------------+---------+

我期待的正确平衡是

+------------+--------------+---------+
| product_id | product_name | balance |
+------------+--------------+---------+
| 111        | Product One  | 9       |
| 222        | Product Two  | 9       |
| 333        | Product Three| 2       |
| 444        | Product Four | 6       |
+------------+--------------+---------+

最后为这个解决方案找到了一个有效的sql,但不确定这是否是最好的方法。我非常关注性能,因为这些表有大量的记录。

SELECT p.product_id,p.product_name,
IFNULL(sum(st.qty_received),0)+IFNULL(sold,0) as balance FROM products p
  LEFT JOIN stock st ON (p.product_id=st.product_id)
  LEFT JOIN (SELECT sa.product_id,sum(sa.qty_sold) as sold FROM sales sa 
     GROUP BY sa.product_id) as t1 ON (t1.product_id=p.product_id)
GROUP BY product_id

任何人都可以建议更好的sql(或更改架构)以获得更好的速度/性能以获得库存平衡?

1 个答案:

答案 0 :(得分:2)

首先,将p.product_name添加到GROUP BY子句。

其次,删除DISTINCT - GROUP BY不需要它。

<强>更新

我尝试了几个查询,发现这个查询最简单:

SELECT 
  p.product_id, 
  p.product_name,
  COALESCE(SUM(qty), 0) as balance 
FROM products p 
LEFT JOIN (
    SELECT product_id, qty_received AS qty FROM stock st UNION ALL 
    SELECT product_id, qty_sold FROM sales st
) AS T 
  ON p.product_id = T.product_id
GROUP BY 
  p.product_id, 
  p.product_name

另外,我认为在所有表中使product_id的数据类型相同是个好主意。它是varchar中的productsint中的sales/stock

我为你做了Fiddle