我正在创建一个设备租赁跟踪系统,该系统管理分配给每个作业的库存,该库存也分配给箱子。
我有下表:
表格框(容器列表,在完成阶段分配给作业,返回时将作业设置为空)
box
job
...
表格代码(股票类型)
code
dept
type
description
...
表格工作(工作)
job
client
name
...
表格订单(与工作相关的订单)
id
job
...
表订单项(与订单相关的项目)
id
order
code
quantity
...
表格满足(完成订单项,应用+船上数量,-退货数量)
id
orderitem
code
quantity
...
表库存(+购买数量,-处置数量)
id
code
quantity
...
表格存储箱(将+或-数量的代码应用于一个存储箱)
id
code
box
quantity
...
当SUM(quantity)> 0(忽略返回的项目)时,我已经生成了一个显示所有满足条件的结果集:
SELECT
'' AS box,
codes.code AS item,
'' AS parent,
codes.code AS code,
codes.dept AS codedept,
codes.type AS codestype,
codes.description AS codedesc,
'' AS make,
'' AS model,
'' AS name,
'' AS serial,
'' AS status,
'' AS LOCATION,
orders.job AS job,
SUM(fulfils.quantity) AS quantity
FROM
fulfils
LEFT JOIN orderitems ON orderitems.id = fulfils.orderitem
LEFT JOIN orders ON orders.id = orderitems.order
LEFT JOIN codes ON codes.code = fulfils.code
WHERE
orders.job = 'TEST'
GROUP BY
fulfils.code,
codes.dept,
codes.type,
codes.description,
orders.job
HAVING
quantity > 0;
这按预期工作。
我现在面临的挑战是根据对工作的分配及其库存箱的总分配(该箱子和代码的库存箱数量的总和)将其分成多个箱子。另外,我还需要包括未分配给任何包装箱的履行总额(数量总和-分配给包装箱的数量总和)。我忽略了与此无关的库存表,仅是库存箱。
假定框“ BOX01”分配给作业“ TEST”,“ BOX01”的总和为3。作业“ TEST”,通过表实现的代码“ PRODUCT01”的总和为5,我希望看到这些查询.job ='TEST'时的结果。
+-------+-----------+-----+----------+
| box | item | ... | quantity |
+-------+-----------+-----+----------+
| BOX01 | PRODUCT01 | ... | 3 |
| | PRODUCT01 | ... | 2 |
+-------+-----------+-----+----------+
我完全愿意进行设计更改。一个简单的解决方法是将“ box”添加到fulfils中,但是我希望在完成库存后在不影响履行表的情况下自由地在各个框之间重新分配库存。
非常感谢。
更新:
这使我走近了,为分配给作业及其中的存货的每个框创建行。现在,我需要从剩余的履行数量中减去装箱数量的总和。
SELECT
stockbox.box AS box,
codes.code AS item,
stockbox.box AS parent,
codes.code AS code,
codes.dept AS codedept,
codes.type AS codestype,
codes.description AS codedesc,
'' AS make,
'' AS model,
'' AS name,
'' AS serial,
'' AS status,
'' AS location,
boxes.job AS job,
SUM(stockbox.quantity) AS quantity
FROM
stockbox
LEFT JOIN codes ON codes.code = stockbox.code
LEFT JOIN boxes ON boxes.box = stockbox.box
WHERE
boxes.job = 'TEST'
GROUP BY
stockbox.box,
codes.code,
codes.dept,
codes.type,
codes.description,
boxes.box
HAVING
quantity > 0
UNION
SELECT
'' AS box,
codes.code AS item,
'' AS parent,
codes.code AS code,
codes.dept AS codedept,
codes.type AS codestype,
codes.description AS codedesc,
'' AS make,
'' AS model,
'' AS name,
'' AS serial,
'' AS status,
'' AS location,
orders.job AS job,
SUM(fulfils.quantity) AS quantity
FROM
fulfils
LEFT JOIN orderitems ON orderitems.id = fulfils.orderitem
LEFT JOIN orders ON orders.id = orderitems.order
LEFT JOIN codes ON codes.code = fulfils.code
WHERE
orders.job = 'TEST'
GROUP BY
fulfils.code,
codes.dept,
codes.type,
codes.description,
orders.job
HAVING
quantity > 0;
礼物(剩下的5个应该是2个):
+-------+-----------+-----+----------+
| box | item | ... | quantity |
+-------+-----------+-----+----------+
| BOX01 | PRODUCT01 | ... | 3 |
| | PRODUCT01 | ... | 5 |
+-------+-----------+-----+----------+
答案 0 :(得分:0)
认为我已经破解了:
SELECT
stockbox.box AS box,
codes.code AS item,
stockbox.box AS parent,
codes.code AS code,
codes.dept AS codedept,
codes.type AS codestype,
codes.description AS codedesc,
'' AS make,
'' AS model,
'' AS name,
'' AS serial,
'' AS status,
'' AS LOCATION,
boxes.job AS job,
SUM(stockbox.quantity) AS quantity
FROM
stockbox
LEFT JOIN codes ON codes.code = stockbox.code
LEFT JOIN boxes ON boxes.box = stockbox.box
WHERE
boxes.job = 'TEST'
GROUP BY
stockbox.box,
codes.code,
codes.dept,
codes.type,
codes.description,
boxes.box
HAVING
quantity > 0
UNION
SELECT
'' AS box,
codes.code AS item,
'' AS parent,
codes.code AS code,
codes.dept AS codedept,
codes.type AS codestype,
codes.description AS codedesc,
'' AS make,
'' AS model,
'' AS name,
'' AS serial,
'' AS status,
'' AS LOCATION,
orders.job AS job,
SUM(fulfils.quantity) - (
SELECT SUM(moo.quantity)
FROM (
SELECT
stockbox.quantity AS quantity
FROM
stockbox
LEFT JOIN boxes boxes2 ON boxes2.box = stockbox.box
WHERE
boxes2.job = 'TEST'
) AS moo
) AS quantity
FROM
fulfils
LEFT JOIN orderitems ON orderitems.id = fulfils.orderitem
LEFT JOIN orders ON orders.id = orderitems.order
LEFT JOIN codes ON codes.code = fulfils.code
WHERE
orders.job = 'TEST'
GROUP BY
fulfils.code,
codes.dept,
codes.type,
codes.description,
orders.job
HAVING
quantity > 0;
似乎可以满足我的需要,并且使我对嵌套选择的总和有了更好的理解。
非常感谢其他解决方案。