简体   繁体   English

跨多个表聚合的查询

[英]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:这是数据库的结构:

DatabaseStructure数据库结构

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材质ID MaterialDescription材质说明
1 1个 Bottle瓶子
2 2个 Box盒子

Table Batch表格批次

BatchID批号 MaterialID材质ID VendorMaterial供应商材料 VendorBatch供应商批次 ExpirationDate截止日期
1000 1000 1 1个 2096027 2096027 00123456 00123456 12/12/2025 12/12/2025
1001 1001 1 1个 2096027 2096027 00987654 00987654 11/11/2026 11/11/2026
1002 1002 2 2个 102400 102400 202400E 202400E 10/10/2023 2023 年 10 月 10 日

Table Movement运动

MovementID运动ID BatchID批号 MovementType运动类型 Quantity数量 CreatedBy由...制作 CreatedOnDate创建日期
1 1个 1000 1000 Good receipt好收据 100 100 user1@email.com user1@email.com 4/10/2022 2022 年 4 月 10 日
2 2个 1000 1000 Inventory withdrawal库存提取 20 20 user2@email.com user2@email.com 4/15/2022 2022 年 4 月 15 日
3 3个 1000 1000 Inventory withdrawal库存提取 25 25 user3@email.com user3@email.com 4/17/2022 2022 年 4 月 17 日
4 4个 1001 1001 Good receipt好收据 100 100 user1@email.com user1@email.com 4/20/2022 2022 年 4 月 20 日
5 5个 1001 1001 Inventory withdrawal库存提取 10 10 user4@email.com user4@email.com 4/26/2022 2022 年 4 月 26 日
6 6个 1002 1002 Good receipt好收据 50 50 user1@email.com user1@email.com 2/26/2022 2/26/2022

Expected query result - total inventory per material:预期查询结果 - 每种材料的总库存:

MaterialDescription材质说明 TotalInventory总库存
Bottle瓶子 145 145
Box盒子 50 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM