从多个批次中减去消耗价值
此问题涉及在多个库存批次 (Pooled_Lots) 之间分配消耗资源 (QuantityConsumed),同时跟踪累计数量、剩余需求以及任何盈余或赤字。
数据结构:
我们有两张桌子:
- Pooled_Lots:包含每个库存批次的详细信息(池、批次、数量)。
- Pool_Conclusion:指定每个池消耗的总数量(PoolId、QuantityConsumed)。
期望的结果:
查询应该为Pooled_Lots
中的每个批次生成一个结果集,包括:
-
Pool
、Lot
、Quantity
、QuantityConsumed
、RunningQuantity
、RemainingDemand
、SurplusOrDeficit
。
解决方法:
递归公用表表达式(CTE)提供了一种有效的解决方案。逻辑如下:
- 使用每个池的第一批来初始化 CTE。
- 对于每个池中的后续批次:
- 如果批次的
RunningQuantity
超过剩余的QuantityConsumed
,则通过减去Quantity
来更新QuantityConsumed
。 - 将
RemainingDemand
计算为QuantityConsumed
与RunningQuantity
之和与当前批次Quantity
之和之间的差值。 - 对于每个池中的最后一批,计算
SurplusOrDeficit
作为RunningQuantity
和RemainingDemand
之间的差异。
- 如果批次的
SQL 查询(说明性):
提供的查询不完整,并且包含一些逻辑不一致的地方。 强大的解决方案需要仔细处理边缘情况(例如,QuantityConsumed
为零或超过池中所有批次的总数)。 需要基于特定的数据库系统(例如 SQL Server、PostgreSQL、MySQL)来设计更正且更高效的查询。 以下是更准确方法的概念概要:
WITH RecursiveCTE AS ( -- Anchor member: Select the first lot for each pool SELECT PL.Pool, PL.Lot, PL.Quantity, PC.QuantityConsumed, PL.Quantity AS RunningQuantity, CASE WHEN PC.QuantityConsumed IS NULL THEN PL.Quantity ELSE PC.QuantityConsumed - PL.Quantity END AS RemainingDemand, 0 AS SurplusOrDeficit, ROW_NUMBER() OVER (PARTITION BY PL.Pool ORDER BY PL.Lot) as rn FROM Pooled_Lots PL LEFT JOIN Pool_Consumption PC ON PL.Pool = PC.PoolId WHERE PL.Lot = 1 --First lot UNION ALL -- Recursive member: Process subsequent lots SELECT PL.Pool, PL.Lot, PL.Quantity, PC.QuantityConsumed, CASE WHEN r.RemainingDemand > PL.Quantity THEN r.RunningQuantity - PL.Quantity ELSE r.RunningQuantity - r.RemainingDemand END AS RunningQuantity, CASE WHEN r.RemainingDemand > PL.Quantity THEN r.RemainingDemand - PL.Quantity ELSE 0 END AS RemainingDemand, CASE WHEN r.rn = (SELECT MAX(rn) FROM RecursiveCTE WHERE Pool = PL.Pool) THEN r.RunningQuantity - r.RemainingDemand ELSE 0 END AS SurplusOrDeficit, r.rn + 1 FROM Pooled_Lots PL INNER JOIN RecursiveCTE r ON PL.Pool = r.Pool AND PL.Lot = r.rn + 1 LEFT JOIN Pool_Consumption PC ON PL.Pool = PC.PoolId ) SELECT * FROM RecursiveCTE ORDER BY Pool, Lot;
结果:
输出将显示每批的详细信息,包括累计RunningQuantity
、剩余RemainingDemand
以及分配后的任何SurplusOrDeficit
。 RunningQuantity
、RemainingDemand
和 SurplusOrDeficit
计算的准确性取决于递归 CTE 中实现的精确逻辑来处理所有可能的场景。 完整且经过测试的解决方案需要了解特定的数据库系统以及用于验证的潜在样本数据。
以上是如何在保持累积值的同时从多行中减去消耗值?的详细内容。更多信息请关注PHP中文网其他相关文章!

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

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

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

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

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

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

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

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


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3汉化版
中文版,非常好用

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中