跨多个表聚合的查询
Query to aggregate across multiple tables
我是 SQL 的新手,我正在尝试创建一个数据库来管理少量库存。这是数据库的结构:
DatabaseStructure
我需要创建一个查询 returns 每个 material 的总库存。因此,第一步是查找与 material 关联的所有批次。其次,查找与每个批次相关的所有动作。然后,将与每个移动相关联的数量相加,但是取决于移动类型(如果是好收据是加法(+),但如果是库存提取是减法(- )).
这是一个包含示例数据和所需结果的表格示例。
Table Material
MaterialID
MaterialDescription
1
Bottle
2
Box
Table 批量
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 移动
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
预期查询结果 - 每个 material 的总库存:
MaterialDescription
TotalInventory
Bottle
145
Box
50
TotalInventory
计算:对于 Bottle 有两个 100 的良好收货移动和三个 20、25 和 10 的提款。因此,总库存将为 (100 +100)-(20+25+10)=145.
感谢您的帮助!
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
;
我是 SQL 的新手,我正在尝试创建一个数据库来管理少量库存。这是数据库的结构:
DatabaseStructure
我需要创建一个查询 returns 每个 material 的总库存。因此,第一步是查找与 material 关联的所有批次。其次,查找与每个批次相关的所有动作。然后,将与每个移动相关联的数量相加,但是取决于移动类型(如果是好收据是加法(+),但如果是库存提取是减法(- )).
这是一个包含示例数据和所需结果的表格示例。
Table Material
MaterialID | MaterialDescription |
---|---|
1 | Bottle |
2 | Box |
Table 批量
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 移动
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 |
预期查询结果 - 每个 material 的总库存:
MaterialDescription | TotalInventory |
---|---|
Bottle | 145 |
Box | 50 |
TotalInventory
计算:对于 Bottle 有两个 100 的良好收货移动和三个 20、25 和 10 的提款。因此,总库存将为 (100 +100)-(20+25+10)=145.
感谢您的帮助!
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
;