对具有相似标识符的两行数据求和
Summing Data on Two Rows with a Similar Identifier
我正在尝试对我们数据库中的一列 'Quantity' 求和,只要项目 ID 本身就是它自己,或者如果它本身前面有一个零(无论 ID 是 2447 还是 02447 作为本示例中的示例)案例):
我从以下开始获取 ID 的总和:
SELECT
"TJ_TransactionJournalDetail"."TLI_ScanCode" As LineItemID,
"TJ_StockInventory"."INV_ScanCode" As InventoryID,
"TJ_TransactionJournalDetail"."TLI_ReceiptAlias" As ReceiptAlias,
"TJ_StockInventory"."INV_Name" As ItemName,
"TJ_TransactionJournalDetail"."TLI_LIT_FK" As LineDiscount,
SUM("TJ_TransactionJournalDetail"."TLI_Quantity") AS Quantity
FROM "TJ_StockInventory" LEFT OUTER JOIN "TJ_TransactionJournalDetail" ON "TJ_StockInventory"."INV_PK" = "TJ_TransactionJournalDetail"."INV_PK"
WHERE ecrs.TJ_TransactionJournalDetail.TLI_StartTime > '2020-01-17 00:00:00.000'
AND ecrs.TJ_TransactionJournalDetail.TLI_EndTime < '2020-01-19 23:59:59.999'
AND INV_DPT_FK = 49
AND "TJ_TransactionJournalDetail"."TLI_LIT_FK" = 1
GROUP BY LineItemID,InventoryID,ReceiptAlias,ItemName,LineDiscount
ORDER BY InventoryID;
这些是我尝试合并的数量的结果:
LineItemID,InventoryID,ReceiptAlias,ItemName,LineDiscount,Quantity
'2447','02447 ','DELI-BEAR CLAW EA','Bear Claw',1,1.0000
'02447','02447 ','DELI-BEAR CLAW EA','Bear Claw',1,30.0000
我在找什么:
'2447','02447 ','DELI-BEAR CLAW EA','Bear Claw',1,31.0000
-或-
'02447','02447 ','DELI-BEAR CLAW EA','Bear Claw',1,31.0000
只要数量是31.
如果 LineItemID 与“0”相同并与另一行的 LineItemID 串联,我基本上想合并两行的数量。或者另一种可能实现它的方法是将所有项目与相同的 InventoryID 合并,但它在 StockInventory table 中,而不是 TransactionJournal table 中,它具有我正在求和的数量。
并尝试了多种解决方案,首先我尝试了 CASE 语句,但无法弄清楚如何跨行应用它:
....
SUM (
CASE WHEN ( "TJ_StockInventory"."INV_ScanCode" = STRING('0',"TJ_TransactionJournalDetail"."TLI_ScanCode",' ') )
THEN "TJ_TransactionJournalDetail"."TLI_Quantity"
ELSE 0 END
) AS Quantity
FROM "TJ_StockInventory" LEFT OUTER JOIN
....
当 InventoryID 相同时,我还尝试按 ItemID 进行分区以合并数量:
....
SUM("TJ_TransactionJournalDetail"."TLI_Quantity") over (partition by "TJ_StockInventory"."INV_ScanCode") AS Quantity
....
但这些解决方案均无效。我选择 "else 0" case 语句只是为了将它缩小到那一项,但在所有情况下它都保持行分开并且没有合并数量。
我看过很多教程,但 none 似乎处理了这个特定案例,但我还没有找到任何暗示解决方案的内容。话虽这么说,但有时我很难全神贯注于数据库编程,而且我对我可能以不正确的方式处理这个问题的想法持开放态度。
我正在寻找的一些伪代码示例
if LineItemID of this row = CONCAT('0', LineItemID) of another row
then sum the quantities of those rows
-或-
if InventoryID of this row and InventoryID of another row are equal
then sum them even if the LineItemIDs are different
任何有助于解决此问题的在线示例或文档的帮助、指示或指导都将非常有用!
谢谢!
您似乎想从聚合中删除 "LineItemID":
SELECT MAX(tjd."TLI_ScanCode") As LineItemID,
si."INV_ScanCode" As InventoryID,
tjd."TLI_ReceiptAlias" As ReceiptAlias,
si."INV_Name" As ItemName,
tjd."TLI_LIT_FK" As LineDiscount,
SUM(tjd."TLI_Quantity") AS Quantity
FROM "TJ_StockInventory" si LEFT OUTER JOIN
"TJ_TransactionJournalDetail" tjd
ON si."INV_PK" = tjd."INV_PK"
WHERE tjd.TLI_StartTime > '2020-01-17' AND
tjd.TLI_EndTime < '2020-01-20' AND
INV_DPT_FK = 49 AND
tjd."TLI_LIT_FK" = 1
GROUP BY InventoryID, ReceiptAlias, ItemName, LineDiscount
ORDER BY InventoryID;
我正在尝试对我们数据库中的一列 'Quantity' 求和,只要项目 ID 本身就是它自己,或者如果它本身前面有一个零(无论 ID 是 2447 还是 02447 作为本示例中的示例)案例):
我从以下开始获取 ID 的总和:
SELECT
"TJ_TransactionJournalDetail"."TLI_ScanCode" As LineItemID,
"TJ_StockInventory"."INV_ScanCode" As InventoryID,
"TJ_TransactionJournalDetail"."TLI_ReceiptAlias" As ReceiptAlias,
"TJ_StockInventory"."INV_Name" As ItemName,
"TJ_TransactionJournalDetail"."TLI_LIT_FK" As LineDiscount,
SUM("TJ_TransactionJournalDetail"."TLI_Quantity") AS Quantity
FROM "TJ_StockInventory" LEFT OUTER JOIN "TJ_TransactionJournalDetail" ON "TJ_StockInventory"."INV_PK" = "TJ_TransactionJournalDetail"."INV_PK"
WHERE ecrs.TJ_TransactionJournalDetail.TLI_StartTime > '2020-01-17 00:00:00.000'
AND ecrs.TJ_TransactionJournalDetail.TLI_EndTime < '2020-01-19 23:59:59.999'
AND INV_DPT_FK = 49
AND "TJ_TransactionJournalDetail"."TLI_LIT_FK" = 1
GROUP BY LineItemID,InventoryID,ReceiptAlias,ItemName,LineDiscount
ORDER BY InventoryID;
这些是我尝试合并的数量的结果:
LineItemID,InventoryID,ReceiptAlias,ItemName,LineDiscount,Quantity
'2447','02447 ','DELI-BEAR CLAW EA','Bear Claw',1,1.0000
'02447','02447 ','DELI-BEAR CLAW EA','Bear Claw',1,30.0000
我在找什么:
'2447','02447 ','DELI-BEAR CLAW EA','Bear Claw',1,31.0000
-或-
'02447','02447 ','DELI-BEAR CLAW EA','Bear Claw',1,31.0000
只要数量是31.
如果 LineItemID 与“0”相同并与另一行的 LineItemID 串联,我基本上想合并两行的数量。或者另一种可能实现它的方法是将所有项目与相同的 InventoryID 合并,但它在 StockInventory table 中,而不是 TransactionJournal table 中,它具有我正在求和的数量。
并尝试了多种解决方案,首先我尝试了 CASE 语句,但无法弄清楚如何跨行应用它:
....
SUM (
CASE WHEN ( "TJ_StockInventory"."INV_ScanCode" = STRING('0',"TJ_TransactionJournalDetail"."TLI_ScanCode",' ') )
THEN "TJ_TransactionJournalDetail"."TLI_Quantity"
ELSE 0 END
) AS Quantity
FROM "TJ_StockInventory" LEFT OUTER JOIN
....
当 InventoryID 相同时,我还尝试按 ItemID 进行分区以合并数量:
....
SUM("TJ_TransactionJournalDetail"."TLI_Quantity") over (partition by "TJ_StockInventory"."INV_ScanCode") AS Quantity
....
但这些解决方案均无效。我选择 "else 0" case 语句只是为了将它缩小到那一项,但在所有情况下它都保持行分开并且没有合并数量。
我看过很多教程,但 none 似乎处理了这个特定案例,但我还没有找到任何暗示解决方案的内容。话虽这么说,但有时我很难全神贯注于数据库编程,而且我对我可能以不正确的方式处理这个问题的想法持开放态度。
我正在寻找的一些伪代码示例
if LineItemID of this row = CONCAT('0', LineItemID) of another row
then sum the quantities of those rows
-或-
if InventoryID of this row and InventoryID of another row are equal
then sum them even if the LineItemIDs are different
任何有助于解决此问题的在线示例或文档的帮助、指示或指导都将非常有用!
谢谢!
您似乎想从聚合中删除 "LineItemID":
SELECT MAX(tjd."TLI_ScanCode") As LineItemID,
si."INV_ScanCode" As InventoryID,
tjd."TLI_ReceiptAlias" As ReceiptAlias,
si."INV_Name" As ItemName,
tjd."TLI_LIT_FK" As LineDiscount,
SUM(tjd."TLI_Quantity") AS Quantity
FROM "TJ_StockInventory" si LEFT OUTER JOIN
"TJ_TransactionJournalDetail" tjd
ON si."INV_PK" = tjd."INV_PK"
WHERE tjd.TLI_StartTime > '2020-01-17' AND
tjd.TLI_EndTime < '2020-01-20' AND
INV_DPT_FK = 49 AND
tjd."TLI_LIT_FK" = 1
GROUP BY InventoryID, ReceiptAlias, ItemName, LineDiscount
ORDER BY InventoryID;