從多批中減去消耗價值
此問題涉及在多個庫存批次 (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-lengthStringStringSuptoPuptOuptoPepePecifiedLimit.2)textisidealforlargetStortStorStoverStoverStorageWithoutAutAdefinedLength.3)charlisfixed-lenftenge,for forConsistentDatalikeCodes.4)

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)

toadduserInmysqleffect和securly,跟隨台詞:1)USEtheCreateUserStattoDaneWuser,指定thehostandastrongpassword.2)GrantNecterAryAryaryPrivilegesSustherthing privilegesgeStatement,usifementStatement,adheringtotheprinciplelastprefilegege.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
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器