3

我目前在 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. 这样的行怎么合并?这只是需要调整的众多查询之一。可能吗?

编辑 - 如果重要的话,类似的材料将始终是基数加上“W”。

请注意,我是 SQL 和 SSRS 的新手,这是我第一次在这里发帖。让我知道是否需要包含任何其他详细信息。

提前致谢。

回答;

仅使用替换,即使使用 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;
4

2 回答 2

0

您可以按此表达式而不是MATERIAL_NUMBER

CASE SUBSTRING(MATERIAL_NUMBER, LEN(MATERIAL_NUMBER), 1)
    WHEN 'W' THEN LEFT(MATERIAL_NUMBER, LEN(MATERIAL_NUMBER) - 1)
    ELSE MATERIAL_NUMBER
END

也就是说,检查最后一个字符是否为W. 如果是,则返回除最后一个字符之外的所有字符,否则返回整个值。

为了避免重复相同的表达式两次(一次 inGROUP BY和一次 in SELECT),您可以使用子选择,例如:

select Issue.MATERIAL_NUMBER_GROUP,
SUM(Issue.SHIPPED_QTY)AS DEMAND_QTY,
Main.QUANTITY_TOTAL_STOCK / SUM(Issue.SHIPPED_QTY) * 122 AS [DOI]

From (
    SELECT
        *,
        CASE SUBSTRING(MATERIAL_NUMBER, LEN(MATERIAL_NUMBER), 1)
            WHEN 'W' THEN LEFT(MATERIAL_NUMBER, LEN(MATERIAL_NUMBER) - 1)
            ELSE MATERIAL_NUMBER
        END AS MATERIAL_NUMBER_GROUP
    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_GROUP,Main.QUANTITY_TOTAL_STOCK
于 2012-07-03T08:24:42.063 回答
0

您需要Issue.MATERIAL_NUMBER在 select 和 group by 中替换为其他内容。其他东西是什么取决于您的数据。

  • 如果它总是 10 位数字,之后忽略任何内容,那么您可以使用substr(Issue.MATERIAL_NUMBER, 1, 10)
  • 如果无关字符始终是 W 并且正确数字中没有 W,那么您可以使用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
于 2012-07-03T01:29:27.723 回答