[英]Query to aggregate across multiple tables
I'm new to SQL, and I'm trying to create a database to manage a small inventory.我是 SQL 的新手,我正在尝试创建一个数据库来管理少量库存。 This is the structure of the db:
这是数据库的结构:
I need to create a query that returns the total inventory per material.我需要创建一个查询来返回每种材料的总库存。 So, the first step would be to look up for all the batches associated with the material.
因此,第一步是查找与物料关联的所有批次。 Second, look up for all the movements associated with each batch.
其次,查找与每个批次相关的所有动作。 Then, sum the quantity associated with each movement, but depending on the movement type (If it is a good receipt is addition (+), but if it is an inventory withdrawal is subtraction (-)).
然后,对与每个移动相关的数量求和,但取决于移动类型(如果是良好收货则加 (+),但如果是库存提取则减 (-))。
Here is an example of the tables with sample data and the desired result.以下是包含示例数据和所需结果的表格示例。
Table Material台面材质
MaterialID![]() |
MaterialDescription![]() |
---|---|
1 ![]() |
Bottle![]() |
2 ![]() |
Box![]() |
Table Batch表格批次
BatchID![]() |
MaterialID![]() |
VendorMaterial![]() |
VendorBatch![]() |
ExpirationDate![]() |
---|---|---|---|---|
1000 ![]() |
1 ![]() |
2096027 ![]() |
00123456 ![]() |
12/12/2025 ![]() |
1001 ![]() |
1 ![]() |
2096027 ![]() |
00987654 ![]() |
11/11/2026 ![]() |
1002 ![]() |
2 ![]() |
102400 ![]() |
202400E ![]() |
10/10/2023 ![]() |
Table Movement表运动
MovementID![]() |
BatchID![]() |
MovementType![]() |
Quantity![]() |
CreatedBy![]() |
CreatedOnDate![]() |
---|---|---|---|---|---|
1 ![]() |
1000 ![]() |
Good receipt![]() |
100 ![]() |
user1@email.com ![]() |
4/10/2022 ![]() |
2 ![]() |
1000 ![]() |
Inventory withdrawal![]() |
20 ![]() |
user2@email.com ![]() |
4/15/2022 ![]() |
3 ![]() |
1000 ![]() |
Inventory withdrawal![]() |
25 ![]() |
user3@email.com ![]() |
4/17/2022 ![]() |
4 ![]() |
1001 ![]() |
Good receipt![]() |
100 ![]() |
user1@email.com ![]() |
4/20/2022 ![]() |
5 ![]() |
1001 ![]() |
Inventory withdrawal![]() |
10 ![]() |
user4@email.com ![]() |
4/26/2022 ![]() |
6 ![]() |
1002 ![]() |
Good receipt![]() |
50 ![]() |
user1@email.com ![]() |
2/26/2022 ![]() |
Expected query result - total inventory per material:预期查询结果 - 每种材料的总库存:
MaterialDescription![]() |
TotalInventory![]() |
---|---|
Bottle![]() |
145 ![]() |
Box![]() |
50 ![]() |
TotalInventory
calculation: for Bottle there are two good receipts movements of 100 and three withdrawals of 20, 25 and 10. So, total inventory will be (100+100)-(20+25+10)=145. TotalInventory
计算:对于Bottle ,有两个 100 的收货移动和三个 20、25 和 10 的提货。因此,总库存将为 (100+100)-(20+25+10)=145。
Thanks for your help!谢谢你的帮助!
select
mat.MaterialDescription,
sum(
case mov.MovementType
when 'Good receipt' then 1
when 'Inventory withdrawal' then -1
else 0 /* don't know what to do for other MovementTypes */
end * mov.Quantity
) as TotalInventory
from
Material as mat
left join Batch as bat on bat.MaterialID = mat.MaterialID
left join Movement as mov on mov.BatchID = bat.BatchID
group by
mat.MaterialDescription
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.