我目前在SSRS中使用此select语句向最终用户报告最近的需求和库存天数。
select Issue.MATERIAL_NUMBER,
SUM(Issue.SHIPPED_QTY)AS DEMAND_QTY,
Main.QUANTITY_TOTAL_STOCK / SUM(Issue.SHIPPED_QTY) * 122 AS [DOI]
From AGS_DATAMART.dbo.GOODS_ISSUE AS Issue
join AGS_DATAMART.dbo.OPR_MATERIAL_DIM AS MAT on MAT.MATERIAL_NUMBER = Issue.MATERIAL_NUMBER
join AGS_DATAMART.dbo.SCE_ECC_MAIN_FINAL_INV_FACT AS MAIN on MAT.MATERIAL_SID = MAIN.MATERIAL_SID
join AGS_DATAMART.dbo.SCE_PLANT_DIM AS PLANT on PLANT.PLANT_SID = MAIN.PLANT_SID
Where Issue.SHIP_TO_CUSTOMER_ID = @CUSTID
and Issue.ACTUAL_PGI_DATE > GETDATE() - 122
and PLANT.PLANT_CODE = @CUSTPLANT
and MAIN.STORAGE_LOCATION = '0001'
Group by Issue.MATERIAL_NUMBER,Main.QUANTITY_TOTAL_STOCK
很简单
但是引起我注意的是,它们具有相似的物料编号,需要将其值组合在一起。
Material | Qty
0242-55161W 1
0242-55161 3
上面的两个物料编号应组合并报告为0242-55161数量4。
如何合并这样的行?这只是许多需要调整的查询之一。可能吗?
编辑-如果重要的话,相似的 Material 将始终是基数加“W”。
请注意,我是SQL和SSRS的新手,这是我第一次在这里发布。
让我知道是否需要包含其他详细信息。
提前致谢。
回答;
仅使用replace,即使使用SUM,它也始终返回2个唯一的行。
我可以使用以下方法获得所需的结果。您能看到这种方法有什么问题吗?
with Issue_Con AS
(
select replace(Issue.MATERIAL_NUMBER,'W','') As [MATERIAL_NUMBER],
Issue.SHIPPED_QTY AS [SHIPPED_QTY]
From AGS_DATAMART.dbo.GOODS_ISSUE AS Issue
Where Issue.SHIP_TO_CUSTOMER_ID = @CUSTSHIP
and Issue.SALES_ORDER_TYPE_CODE = 'ZTPC'
and Issue.ACTUAL_PGI_DATE > GETDATE() - 122
)
select Issue_Con.MATERIAL_NUMBER,
SUM(Issue_Con.SHIPPED_QTY)AS [DEMAND_QTY],
Main_Con.QUANTITY_TOTAL_STOCK / SUM(Issue_Con.SHIPPED_QTY) * 122 AS [DOI]
From Issue_Con
join Main_Con on Main_Con.MATERIAL_Number = Issue_Con.MATERIAL_Number
Group By Issue_Con.MATERIAL_NUMBER, Main_Con.QUANTITY_TOTAL_STOCK;
最佳答案
您需要用其他方式替换select和group中的Issue.MATERIAL_NUMBER
。还有什么取决于您的数据。
substr(Issue.MATERIAL_NUMBER, 1, 10)
replace(Issue.MATERIAL_NUMBER, 'W', '')
case when patindex('%[A-Za-z]%', Issue.MATERIAL_NUMBER) = 0 then Issue.MATERIAL_NUMBER else substr(Issue.MATERIAL_NUMBER, 1, patindex('%[A-Za-z]%', Issue.MATERIAL_NUMBER)) end
关于sql - 相似行的T-SQL总和值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11303126/