0

我有 2 个表:CUST_DETAILSwith columnCUST_REF_IDCUST_MERGEwith columns NEW_CUST_REF_IDand OLD_CUST_REF_ID,下面的脚本将检查两个列是否CUST_MERGE具有相同的国家/地区 id 与相应的列CUST_DETAILS,并返回 Y,否则返回 N

SELECT
   NVL
      ((SELECT 'Y' FROM DUAL WHERE EXISITS
       (
        SELECT CTRY_ID FROM CUST_DETAILS cust_dtl
        INNER JOIN CUST_MERGE cust_merge
        ON cust_dtl.CUST_REF_ID=cust_merge.NEW_CUST_REF_ID
        AND cust_dtl.CUST_REF_ID=cust_merge.OLD_CUST_REF_ID
        GROUP BY CTRY_ID
        HAVING COUNT(CTRY_ID)>1
        )),'N') AS SAME_CTRY_ID
FROM DUAL;

两个表中的数据都具有相同的 country_id,这意味着预期结果是“Y”。但是现在返回“N”,除非我将条件从 更改为 ,否则ANDOR会按预期工作,即使子查询也返回正确的国家/地区 ID

样本数据

表 CUST_MERGE

 NEW_CUST_REF_ID                        OLD_CUST_REF_ID

 B5000                                   B6000

 B5000                                   A6000

表 CUST_DETAILS

 CUST_REF_ID                CTRY_ID  
 B5000                       US

 B6000                       US   

 A6000                       JP

执行第一行CUST_MERGE,B5000和B6000,返回Y,因为ctry_id相同(美国) 执行第二行CUST_MERGE,B5000和A6000,返回N,因为ctry_id不同(美国,日本)

4

2 回答 2

0
SELECT
m.CUST_REF_ID
FROM
CUST_MERGE M
INNER JOIN 
CUST_DETAILS D1 on 
(m.CUST_REF_ID=d1.NEW_CUST_REF_ID)
INNER JOIN 
CUST_DETAILS D2 on 
(m.CUST_REF_ID=d2.OLD_CUST_REF_ID)
WHERE d1.CTRY_ID=d2.CTRY_ID

将其包含在您的 Y/N 代码中。(我认为)或者也许

SELECT 
crid
from
(
SELECT
OLD_CUST_REF_ID crid
from CUST_MERGE
UNION ALL
SELECT
NEW_CUST_REF_ID 
from CUST_MERGE
) as x inner join
CUST_DETAILS on x.crid=CUST_DETAILS.CUST_REF_ID 
group by CRID
HAVING (COUNT DISTINCT CTRY_ID)>1
于 2018-05-24T08:35:22.257 回答
0

使用此查询获取标志:

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
于 2018-05-24T08:40:53.267 回答