查询以跨多个表汇总

发布于 2025-01-24 06:21:37 字数 2960 浏览 0 评论 0 原文

我是SQL的新手,我正在尝试创建一个数据库来管理小型库存。这是db的结构:

databasestructure

我需要创建一个返回总库存的查询每种材料。因此,第一步是查找与材料相关的所有批次。其次,查找与每批相关的所有动作。然后,将与每个运动关联的数量汇总,但是取决于运动类型(如果是一个好的收据,则是添加的(+),但是如果库存提取是撤回的,则是减去( - ))。

这是具有样本数据和所需结果的表的示例。

材料

1 strong

批次

batchid Metterialid vendormatial vendorial vendorbatch Expirationdate DateDate Date
1 2096027 00123456 12/12/2025 1001
1 2096027 0096027 00987654 11/11/11/2026
1000 10022400 strong < / 202400E 10/10/2023

运动

MovementId batchId sovemttype 数量 创建的 creat dycondate
1000 100 良好收据 -protection” class =” __ cf_email__ cf_email__ cf_email__ =“ 205555521160454D41494C0E434F4D”> [email&nbsp; pretand] 4/10/2022
2 1000 库存提款 20 [email&nbsp; prectioned] 4/15/2022
3 1000 库存提款 25 [电子邮件&nbsp;受保护] 4/17/2022
4 1001 好收据 100 4/20/2022
5 1001 库存提款 10 4/26/2022
6 1002 好收据 50 2/26/2022

预期查询结果 - 每份材料的总库存:

材料数量 总计库
145
50

codalinventory 计算:对于,有两个好的收据动作100和三个撤回20、25和10。因此,总库存将为(100+100) - (20+25+10)= 145。

感谢您的帮助!

I'm new to SQL, and I'm trying to create a database to manage a small inventory. 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 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 [email protected] 4/10/2022
2 1000 Inventory withdrawal 20 [email protected] 4/15/2022
3 1000 Inventory withdrawal 25 [email protected] 4/17/2022
4 1001 Good receipt 100 [email protected] 4/20/2022
5 1001 Inventory withdrawal 10 [email protected] 4/26/2022
6 1002 Good receipt 50 [email protected] 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.

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪少女 2025-01-31 06:21:37
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
;
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
;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文