如何通过计算IN和OUT之差来计算物理量
How to calculating physical quantity by calculating the difference between IN and OUT
我正在开发一个库存控制应用程序,我正在使用 MySQL RDBMS 和 InnoDB 引擎,目前我想生成一个报告,通过计算产品之间的差异来计算产品的物理数量IN_OUT 列的 SUM 使用 MySQL 查询。
我使用 in_out_products 中的值“1”作为 IN,“0”作为 OUT
所以我需要一个在 IN 和 OUT 之间有区别的列
IN - OUT =实物数量 每件商品
以更清楚的方式,我的意思是 SUM(quantity) where in_out=1 和 SUM(quantity) where in_out=0 ,in_out 列在 in_out_products table
第一个table
select * from products;
+------------+--------------+-------------+----------------+-------+------------+
| product_id | product_code | part_number | product_name | brand | location |
+------------+--------------+-------------+----------------+-------+------------+
| 1 | 100100 | 100200 | RICHO maginate | RICHO | location 4 |
| 2 | 1112211 | 100300 | RICHO Cyan | RICHO | location 4 |
| 3 | 111333 | 100400 | RICHO Yellow | RICHO | location 4 |
+------------+--------------+-------------+----------------+-------+------------+
秒table
+-----------+------------+--------------+-------------+----------+--------+
| record_id | product_id | product_code | part_number | quantity | in_out |
+-----------+------------+--------------+-------------+----------+--------+
| 1 | 1 | 100100 | 100200 | 10 | 1 |
| 2 | 1 | 100100 | 100200 | 10 | 1 |
| 3 | 1 | 100100 | 100200 | 30 | 1 |
| 4 | 2 | 1112211 | 100300 | 10 | 1 |
| 5 | 2 | 1112211 | 100300 | 10 | 1 |
| 6 | 2 | 1112211 | 100300 | 10 | 1 |
| 7 | 2 | 1112211 | 100300 | 10 | 1 |
| 8 | 3 | 111333 | 100400 | 10 | 1 |
| 9 | 3 | 111333 | 100400 | 10 | 1 |
| 10 | 3 | 111333 | 100400 | 10 | 1 |
+-----------+------------+--------------+-------------+----------+--------+
现在我想创建一个 SQL 查询来显示当前数量的所有产品,我没有做到这一点,到目前为止我设法通过这个查询获得了 IN 数量。
SQL 查询计算每个产品的 IN 数量:
select p.product_id,
p.product_id,
p.product_code,
p.product_name,
SUM(io.quantity) as physical_quantity
from products as p,
in_out_products as io where p.product_id=io.product_id and io.deleted_at is null and in_out=1 group by p.product_id;
结果
+------------+------------+--------------+----------------+-------------------+
| product_id | product_id | product_code | product_name | physical_quantity |
+------------+------------+--------------+----------------+-------------------+
| 1 | 1 | 100100 | RICHO maginate | 50 |
| 2 | 2 | 1112211 | RICHO Cyan | 40 |
| 3 | 3 | 111333 | RICHO Yellow | 30 |
+------------+------------+--------------+----------------+-------------------+
如果没记错你需要使用Conditional Aggregate
select p.product_id,
p.product_id,
p.product_code,
p.product_name,
SUM(io.quantity) as physical_quantity,
SUM(case when in_out=1 then io.quantity else 0 END) -
SUM(case when in_out=0 then io.quantity else 0 END) AS Difference
from products as p
Join in_out_products as io
on p.product_id=io.product_id
Where io.deleted_at is null
and in_out=1
group by p.product_id;
我正在开发一个库存控制应用程序,我正在使用 MySQL RDBMS 和 InnoDB 引擎,目前我想生成一个报告,通过计算产品之间的差异来计算产品的物理数量IN_OUT 列的 SUM 使用 MySQL 查询。
我使用 in_out_products 中的值“1”作为 IN,“0”作为 OUT
所以我需要一个在 IN 和 OUT 之间有区别的列
IN - OUT =实物数量 每件商品
以更清楚的方式,我的意思是 SUM(quantity) where in_out=1 和 SUM(quantity) where in_out=0 ,in_out 列在 in_out_products table
第一个table
select * from products;
+------------+--------------+-------------+----------------+-------+------------+
| product_id | product_code | part_number | product_name | brand | location |
+------------+--------------+-------------+----------------+-------+------------+
| 1 | 100100 | 100200 | RICHO maginate | RICHO | location 4 |
| 2 | 1112211 | 100300 | RICHO Cyan | RICHO | location 4 |
| 3 | 111333 | 100400 | RICHO Yellow | RICHO | location 4 |
+------------+--------------+-------------+----------------+-------+------------+
秒table
+-----------+------------+--------------+-------------+----------+--------+
| record_id | product_id | product_code | part_number | quantity | in_out |
+-----------+------------+--------------+-------------+----------+--------+
| 1 | 1 | 100100 | 100200 | 10 | 1 |
| 2 | 1 | 100100 | 100200 | 10 | 1 |
| 3 | 1 | 100100 | 100200 | 30 | 1 |
| 4 | 2 | 1112211 | 100300 | 10 | 1 |
| 5 | 2 | 1112211 | 100300 | 10 | 1 |
| 6 | 2 | 1112211 | 100300 | 10 | 1 |
| 7 | 2 | 1112211 | 100300 | 10 | 1 |
| 8 | 3 | 111333 | 100400 | 10 | 1 |
| 9 | 3 | 111333 | 100400 | 10 | 1 |
| 10 | 3 | 111333 | 100400 | 10 | 1 |
+-----------+------------+--------------+-------------+----------+--------+
现在我想创建一个 SQL 查询来显示当前数量的所有产品,我没有做到这一点,到目前为止我设法通过这个查询获得了 IN 数量。
SQL 查询计算每个产品的 IN 数量:
select p.product_id,
p.product_id,
p.product_code,
p.product_name,
SUM(io.quantity) as physical_quantity
from products as p,
in_out_products as io where p.product_id=io.product_id and io.deleted_at is null and in_out=1 group by p.product_id;
结果
+------------+------------+--------------+----------------+-------------------+
| product_id | product_id | product_code | product_name | physical_quantity |
+------------+------------+--------------+----------------+-------------------+
| 1 | 1 | 100100 | RICHO maginate | 50 |
| 2 | 2 | 1112211 | RICHO Cyan | 40 |
| 3 | 3 | 111333 | RICHO Yellow | 30 |
+------------+------------+--------------+----------------+-------------------+
如果没记错你需要使用Conditional Aggregate
select p.product_id,
p.product_id,
p.product_code,
p.product_name,
SUM(io.quantity) as physical_quantity,
SUM(case when in_out=1 then io.quantity else 0 END) -
SUM(case when in_out=0 then io.quantity else 0 END) AS Difference
from products as p
Join in_out_products as io
on p.product_id=io.product_id
Where io.deleted_at is null
and in_out=1
group by p.product_id;