简体   繁体   中英

SQL to find the match record based on the priority defined

I have to build a logic to update one column SC_LEVI_LVL IN GCC_MAST.CLNT_MAST based on the match found on the below criteria:

(I am just looking at the select clause and I will update it later using update strategy in informatica)

The Joins needs to be made on three tables:

for Priority 1 the join needs to be made on below two tables:

GCC_LANDING.CB_AT_LIS CB
    GCC_MAST.CLNT_MAST TM

For Priority 2 the join needs to be made on below 3 tables:

GCC_LANDING.CB_AT_LIS CB
GCC_MAST.CLNT_MAST TM
GCC_LANDING.GRID_MATRIX GM

PRIORITY 1:

CB.REL_COUNTRY=TM.REL_COUNTRY
AND CB.GRID=TM.GRID
WHERE TM.SCH_LEG_ENTY NOT LIKE '%BASE%'
AND CB.LAT='Y'

Expected result: 在此处输入图像描述

PRIORITY 2:

CB.GRID=GM.GRID
AND CB.REL_COUNTRY=TM.REL_COUNTRY
AND GM.SUR_GRD=TM.GRID
WHERE TM.SCH_LEG_ENTY NOT LIKE '%BASE%'
AND TM.CUS LIKE '% BGM%'
AND CB.GRID <> TM.GRID
AND TM.CUST_FLG='N'

Expected results: 在此处输入图像描述

(IF MULTIPLE RECORDS FOUND PARTITION BY ROW_NUMBER=1)

All i can think of is putting all of them in a single query with the Joins on all tables ( combining P1 and P2) and then a case statement in the beginning. But, I don't think that will be the correct approach.

Can someone guide me to the approach on how can i proceed? I am pretty new to this and will update the post with whatever i come up with.

Desired result should be the value of SC_LEVI_LVL IN GCC_MAST.CLNT_MAST based on P1 first and if no match for P1 then for P2

Thanks!

-- here is the combination of the two - priority_one and priority_two
WITH
    landing As
        (
            Select 'NZ' "REL_COUNTRY", 12345 "GRID", 'Y' "LAT", 'ABC' "SC_LEVI_LVL" From Dual
          UNION
            Select 'MO' "REL_COUNTRY", 44444 "GRID", 'Y' "LAT", 'XYZ' "SC_LEVI_LVL" From Dual
          UNION
            Select 'PH' "REL_COUNTRY", 55555 "GRID", 'Y' "LAT", 'ERT' "SC_LEVI_LVL" From Dual
          UNION
            Select 'PH' "REL_COUNTRY", 78786 "GRID", 'Y' "LAT", 'ERT' "SC_LEVI_LVL" From Dual
        ),
    clnt As
        (
            Select 'ZA' "REL_COUNTRY", 56565 "GRID", 'HBASEU' "SCH_LEG_ENTY", 'TFGD' "CUS", 'N' "CUST_FLG" From Dual
          UNION
            Select 'NZ' "REL_COUNTRY", 12345 "GRID", 'DFGT' "SCH_LEG_ENTY", 'HBGM' "CUS", 'N' "CUST_FLG" From Dual
          UNION
            Select 'MO' "REL_COUNTRY", 44444 "GRID", 'DFGTU' "SCH_LEG_ENTY", 'HBGM' "CUS", 'N' "CUST_FLG" From Dual
          UNION
            Select 'PH' "REL_COUNTRY", 99999 "GRID", 'DFGTU' "SCH_LEG_ENTY", 'HBGM' "CUS", 'N' "CUST_FLG" From Dual
        ),
    matrix As
        ( Select 99999 "SUR_GRID", 78786 "GRID" From Dual ),
    priority_one As 
        (
            SELECT
                'ONE' "WHICH_ONE",
                cb.REL_COUNTRY "REL_COUNTRY",
                cb.GRID "GRID",
                cb.LAT "LAT",
                cb.SC_LEVI_LVL "SC_LEVI_LVL"
            FROM
                landing cb
            INNER JOIN
                clnt tm ON(CB.REL_COUNTRY=TM.REL_COUNTRY And cb.GRID = tm.GRID)
            WHERE 
                tm.SCH_LEG_ENTY NOT LIKE '%BASE%' And 
                CB.LAT = 'Y'
        ),
    priority_two As
        (
            SELECT
                'TWO' "WHICH_ONE",
                cb.REL_COUNTRY "REL_COUNTRY",
                cb.GRID "GRID",
                cb.LAT "LAT",
                cb.SC_LEVI_LVL "SC_LEVI_LVL"
            FROM
                matrix gm
            INNER JOIN
                landing cb ON(cb.GRID = gm.GRID)
            INNER JOIN
                clnt tm ON(cb.REL_COUNTRY = tm.REL_COUNTRY And gm.SUR_GRID = tm.GRID)
            WHERE 
                tm.SCH_LEG_ENTY NOT LIKE '%BASE%' And 
                cb.GRID <> tm.GRID And 
                tm.CUST_FLG = 'N'
        )
    SELECT WHICH_ONE, REL_COUNTRY, GRID, LAT, SC_LEVI_LVL From priority_one 
  UNION ALL 
    SELECT WHICH_ONE, REL_COUNTRY, GRID, LAT, SC_LEVI_LVL FROM priority_two
--
-- Result:
--
-- WHICH_ONE REL_COUNTRY       GRID LAT SC_LEVI_LVL
-- ONE       MO               44444 Y   XYZ       
-- ONE       NZ               12345 Y   ABC       
-- TWO       PH               78786 Y   ERT       

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM