Используйте этот запрос, чтобы получить флаг:
SQL> WITH CUST_MERGE( NEW_CUST_REF_ID, OLD_CUST_REF_ID) AS
2 (SELECT 'B5000', 'B6000' FROM dual UNION ALL
3 SELECT 'A5000', 'A6000' FROM dual),
4 CUST_DETAILS (CUST_REF_ID,CTRY_ID) AS
5 ( SELECT 'B5000', 'US' FROM dual UNION ALL
6 SELECT 'B6000', 'US' FROM dual UNION ALL
7 SELECT 'A5000', 'UK' FROM dual UNION ALL
8 SELECT 'A6000', 'JP' FROM dual)
----------------------------
----End of Data Preparation
----------------------------
9 SELECT cm.new_cust_ref_id,
10 cm.old_cust_ref_id,
11 dc.ctry_id,
12 dc1.ctry_id,
13 CASE
14 WHEN dc.ctry_id = dc1.ctry_id THEN
15 'Y'
16 ELSE
17 'N'
18 END AS flag
19 FROM cust_merge cm
20 JOIN cust_details dc
21 ON cm.new_cust_ref_id = dc.cust_ref_id
22 JOIN cust_details dc1
23 ON cm.old_cust_ref_id = dc1.cust_ref_id;
Выходные данные
NEW_CUST_REF_ID OLD_CUST_REF_ID CTRY_ID CTRY_ID FLAG
--------------- --------------- ------- ------- ----
B5000 B6000 US US Y
A5000 A6000 UK JP N
Запрос для ваших таблиц выглядит так:
SELECT cm.new_cust_ref_id,
cm.old_cust_ref_id,
dc.ctry_id,
dc1.ctry_id,
CASE
WHEN dc.ctry_id = dc1.ctry_id THEN
'Y'
ELSE
'N'
END AS flag
FROM cust_merge cm
JOIN cust_details dc
ON cm.new_cust_ref_id = dc.cust_ref_id
JOIN cust_details dc1
ON cm.old_cust_ref_id = dc1.cust_ref_id;
Обновление : С новыми данными результаты будут такими, как ожидалось:
SQL> WITH CUST_MERGE( NEW_CUST_REF_ID, OLD_CUST_REF_ID) AS
2 (SELECT 'B5000', 'B6000' FROM dual UNION ALL
3 SELECT 'B5000', 'A6000' FROM dual),
4 CUST_DETAILS (CUST_REF_ID,CTRY_ID) AS
5 ( SELECT 'B5000', 'US' FROM dual UNION ALL
6 SELECT 'B6000', 'US' FROM dual UNION ALL
7 SELECT 'A6000', 'JP' FROM dual)
8 SELECT cm.new_cust_ref_id,
9 cm.old_cust_ref_id,
10 dc.ctry_id,
11 dc1.ctry_id,
12 CASE
13 WHEN dc.ctry_id = dc1.ctry_id THEN
14 'Y'
15 ELSE
16 'N'
17 END AS flag
18 FROM cust_merge cm
19 JOIN cust_details dc
20 ON cm.new_cust_ref_id = dc.cust_ref_id
21 JOIN cust_details dc1
22 ON cm.old_cust_ref_id = dc1.cust_ref_id;
Вывод:
NEW_CUST_REF_ID OLD_CUST_REF_ID CTRY_ID CTRY_ID FLAG
--------------- --------------- ------- ------- ----
B5000 B6000 US US Y
B5000 A6000 US JP N