搜索
首页数据库mysql教程如何使用 SQL 递归 CTE 减少合并批次数量?

How to Reduce Pooled Lot Quantity Using SQL Recursive CTE?

使用 SQL 递归 CTE 减少合并批次数量

此 SQL Server 2005 解决方案采用递归公用表表达式 (CTE),根据消耗数据迭代减少合并批次数量。 该流程可有效处理池内多个批次的消耗量超过可用数量的情况。

这是 SQL 代码:

-- Sample data setup
DECLARE @Pooled_Lots AS TABLE (
  Id INT,
  Pool INT,
  Lot INT,
  Quantity INT
);

INSERT INTO @Pooled_Lots (Id, Pool, Lot, Quantity) VALUES
(1, 1, 1, 5),
(2, 1, 2, 10),
(3, 1, 3, 4),
(4, 2, 1, 7),
(5, 3, 1, 1),
(6, 3, 2, 5);

DECLARE @Pool_Consumption AS TABLE (
  Id INT,
  PoolId INT,
  QuantityConsumed INT
);

INSERT INTO @Pool_Consumption (Id, PoolId, QuantityConsumed) VALUES
(1, 1, 17),
(2, 2, 8),
(3, 3, 10);


-- Recursive CTE to calculate remaining quantities
WITH LotQuantities AS (
    SELECT 
        PL.Pool, 
        PL.Lot, 
        PL.Quantity, 
        PC.QuantityConsumed,
        PL.Quantity as RunningQuantity,
        PC.QuantityConsumed as RemainingDemand,
        CASE WHEN PL.Quantity >= PC.QuantityConsumed THEN 0 ELSE PC.QuantityConsumed - PL.Quantity END as SurplusOrDeficit
    FROM @Pooled_Lots PL
    LEFT JOIN @Pool_Consumption PC ON PL.Pool = PC.PoolId
    WHERE PL.Lot = 1 -- Start with the first lot in each pool

    UNION ALL

    SELECT 
        lq.Pool, 
        PL.Lot, 
        PL.Quantity, 
        lq.QuantityConsumed,
        CASE WHEN lq.RemainingDemand > PL.Quantity THEN lq.RunningQuantity + PL.Quantity ELSE lq.QuantityConsumed END,
        CASE WHEN lq.RemainingDemand > PL.Quantity THEN lq.RemainingDemand - PL.Quantity ELSE 0 END,
        CASE WHEN lq.RemainingDemand > PL.Quantity THEN 0 ELSE lq.RemainingDemand - (lq.RunningQuantity + PL.Quantity) END
    FROM LotQuantities lq
    JOIN @Pooled_Lots PL ON lq.Pool = PL.Pool AND PL.Lot = lq.Lot + 1
    WHERE lq.RemainingDemand > 0
)
SELECT * FROM LotQuantities;

生成的数据集显示消耗扣除后每批的剩余数量。 CTE 递归地处理每个池中的批次,直到完全考虑到消耗的数量。 请注意,SurplusOrDeficit 中的负值表示不足。

以上是如何使用 SQL 递归 CTE 减少合并批次数量?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
MySQL字符串类型:存储,性能和最佳实践MySQL字符串类型:存储,性能和最佳实践May 10, 2025 am 12:02 AM

mySqlStringTypesimpactStorageAndPerformanCeaseAsfollows:1)长度,始终使用theSamestoragespace,whatcanbefasterbutlessspace-felfficity.2)varCharisvariable varcharisvariable length,morespace-morespace-morespace-effficitybuteftife buteftife butfority butfority textifforlyslower.3)

了解MySQL字符串类型:VARCHAR,文本,char等了解MySQL字符串类型:VARCHAR,文本,char等May 10, 2025 am 12:02 AM

mySqlStringTypesIncludeVarChar,文本,char,enum和set.1)varCharisVersAtileForvariable-lengthStringStringSuptOptoPeptoPepecifientlimit.2)textisidealforlargetStortStorStoverStorextorewiteWithoutAdefinedLengthl.3)charlisfixed-Length

MySQL中的字符串数据类型是什么?MySQL中的字符串数据类型是什么?May 10, 2025 am 12:01 AM

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

如何向新的MySQL用户授予权限如何向新的MySQL用户授予权限May 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

如何在MySQL中添加用户:逐步指南如何在MySQL中添加用户:逐步指南May 09, 2025 am 12:14 AM

toadduserInmysqleffectection andsecrely,theTheSepsps:1)USEtheCreateuserStattoDaneWuser,指定thehostandastrongpassword.2)GrantNectalRevileSaryPrivilegesSustate,usiveleanttatement,AdheringTotheTeprinciplelastPrevilegege.3)

mysql:添加具有复杂权限的新用户mysql:添加具有复杂权限的新用户May 09, 2025 am 12:09 AM

toaddanewuserwithcomplexpermissionsinmysql,loldtheSesteps:1)创建eTheEserWithCreateuser'newuser'newuser'@''localhost'Indedify'pa ssword';。2)GrantreadAccesstoalltablesin'mydatabase'withGrantSelectOnMyDatabase.to'newuser'@'localhost';。3)GrantWriteAccessto'

mysql:字符串数据类型和coltrationsmysql:字符串数据类型和coltrationsMay 09, 2025 am 12:08 AM

MySQL中的字符串数据类型包括CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT,排序规则(Collations)决定了字符串的比较和排序方式。1.CHAR适合固定长度字符串,VARCHAR适合可变长度字符串。2.BINARY和VARBINARY用于二进制数据,BLOB和TEXT用于大对象数据。3.排序规则如utf8mb4_unicode_ci忽略大小写,适合用户名;utf8mb4_bin区分大小写,适合需要精确比较的字段。

MySQL:我应该在Varchars上使用什么长度?MySQL:我应该在Varchars上使用什么长度?May 09, 2025 am 12:06 AM

最佳的MySQLVARCHAR列长度选择应基于数据分析、考虑未来增长、评估性能影响及字符集需求。1)分析数据以确定典型长度;2)预留未来扩展空间;3)注意大长度对性能的影响;4)考虑字符集对存储的影响。通过这些步骤,可以优化数据库的效率和扩展性。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。