SQL - 匹配 2 列并将结果查看到另一列

SQL - Match 2 columns and view result to another to column

我有以下查询,我需要在创建新列名称的位置完成查询 MATCH_RESULT 比较 PH_NAME 和 MARA 之间的数据。[Material 描述] 并判断是否匹配。

SELECT DISTINCT 
                  dbo.MARA.Material AS MATERIAL_CODE, dbo.MARA.[Material Description] AS MATERIAL_NAME, dbo.MVKE.[Product Hierarchy] AS PH_CODE, dbo.PH.PH_NAME, dbo.BRAND.B_NAME AS BRAND_NAME, 
                  dbo.BRAND.SB_NAME AS SUBBRAND_NAME, CONCAT(MARA.[Material Description], PH_NAME) AS MATCH_RESULT
FROM     dbo.MARA INNER JOIN
                  dbo.MVKE ON dbo.MARA.Material = dbo.MVKE.Material INNER JOIN
                  dbo.PH ON dbo.MVKE.[Product Hierarchy] = dbo.PH.PH_CODE INNER JOIN
                  dbo.BRAND ON dbo.MARA.Material = dbo.BRAND.Material
WHERE  (dbo.MARA.MTyp = 'ZFPR') AND (dbo.MVKE.DChl = '00')

如果我理解你的问题,你可以在 SELECT.

中使用 CASE 语句

例如

CASE WHEN PH.PH_NAME = MARA.[Material Description] THEN 'Match' ELSE 'No Match' END MATCH_RESULT

这会将列 PH.PH_NAME 与 MARA 进行比较。[Material 说明] 和 return 'Match' 如果它们相等,则 'No Match' 否则。

所以:

SELECT DISTINCT 
                  dbo.MARA.Material AS MATERIAL_CODE, dbo.MARA.[Material Description] AS MATERIAL_NAME, dbo.MVKE.[Product Hierarchy] AS PH_CODE, dbo.PH.PH_NAME, dbo.BRAND.B_NAME AS BRAND_NAME, 
                  dbo.BRAND.SB_NAME AS SUBBRAND_NAME, 
                  CASE WHEN dbo.PH.PH_NAME = MARA.[Material Description] THEN 'Match' ELSE 'No Match' END MATCH_RESULT
FROM     dbo.MARA INNER JOIN
                  dbo.MVKE ON dbo.MARA.Material = dbo.MVKE.Material INNER JOIN
                  dbo.PH ON dbo.MVKE.[Product Hierarchy] = dbo.PH.PH_CODE INNER JOIN
                  dbo.BRAND ON dbo.MARA.Material = dbo.BRAND.Material
WHERE  (dbo.MARA.MTyp = 'ZFPR') AND (dbo.MVKE.DChl = '00')