Home > Software design >  SQL - Match 2 columns and view result to another to column
SQL - Match 2 columns and view result to another to column

Time:05-06

I have below query that I need to complete the query in where to make new column name MATCH_RESULT compare data between PH_NAME and MARA.[Material Description] and say it match or not.

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')

CodePudding user response:

If I'm understanding your question, you can use a CASE statement within your SELECT.

e.g.

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

This compares the column PH.PH_NAME to MARA.[Material Description] and return 'Match' if they are equal, 'No Match' otherwise.

So:

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')
  • Related