SQL 根据定义的优先级查找匹配记录
SQL to find the match record based on the priority defined
我必须构建一个逻辑来更新一列 SC_LEVI_LVL IN GCC_MAST.CLNT_MAST
基于以下条件找到的匹配项:
(我只是在看select子句,稍后我会使用informatica中的更新策略更新它)
需要在三个表上进行连接:
对于优先级 1,需要在以下两个表上进行连接:
GCC_LANDING.CB_AT_LIS CB
GCC_MAST.CLNT_MAST TM
对于优先级 2,需要在以下 3 个表上进行连接:
GCC_LANDING.CB_AT_LIS CB
GCC_MAST.CLNT_MAST TM
GCC_LANDING.GRID_MATRIX GM
优先级 1:
CB.REL_COUNTRY=TM.REL_COUNTRY
AND CB.GRID=TM.GRID
WHERE TM.SCH_LEG_ENTY NOT LIKE '%BASE%'
AND CB.LAT='Y'
预期结果:
优先级 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'
预期结果:
(如果多条记录发现分区 ROW_NUMBER=1)
我能想到的就是将它们全部放在一个查询中,并在所有表上进行联接(合并 P1 和 P2),然后在开头使用 case 语句。但是,我认为这不是正确的方法。
有人可以指导我如何进行吗?我对此很陌生,会用我想出的任何东西更新 post。
期望的结果应该是 SC_LEVI_LVL IN GCC_MAST.CLNT_MAST 的值,首先基于 P1,如果 P1 不匹配,则 P2
谢谢!
-- 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
我必须构建一个逻辑来更新一列 SC_LEVI_LVL IN GCC_MAST.CLNT_MAST
基于以下条件找到的匹配项:
(我只是在看select子句,稍后我会使用informatica中的更新策略更新它)
需要在三个表上进行连接:
对于优先级 1,需要在以下两个表上进行连接:
GCC_LANDING.CB_AT_LIS CB
GCC_MAST.CLNT_MAST TM
对于优先级 2,需要在以下 3 个表上进行连接:
GCC_LANDING.CB_AT_LIS CB
GCC_MAST.CLNT_MAST TM
GCC_LANDING.GRID_MATRIX GM
优先级 1:
CB.REL_COUNTRY=TM.REL_COUNTRY
AND CB.GRID=TM.GRID
WHERE TM.SCH_LEG_ENTY NOT LIKE '%BASE%'
AND CB.LAT='Y'
预期结果:
优先级 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'
预期结果:
(如果多条记录发现分区 ROW_NUMBER=1)
我能想到的就是将它们全部放在一个查询中,并在所有表上进行联接(合并 P1 和 P2),然后在开头使用 case 语句。但是,我认为这不是正确的方法。
有人可以指导我如何进行吗?我对此很陌生,会用我想出的任何东西更新 post。
期望的结果应该是 SC_LEVI_LVL IN GCC_MAST.CLNT_MAST 的值,首先基于 P1,如果 P1 不匹配,则 P2
谢谢!
-- 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