使用 NVL、GROUP BY、HAVING COUNT 检查记录是否相同
Use NVL, GROUP BY, HAVING COUNT to check if record are same
我有 2 个表:CUST_DETAILS
列 CUST_REF_ID
和
CUST_MERGE
列 NEW_CUST_REF_ID
和 OLD_CUST_REF_ID
,下面的脚本将检查 CUST_MERGE
中的两列是否与 CUST_DETAILS
中的相应列具有相同的国家/地区 ID,并且 return Y,否则 return 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' returned,除非我将条件从 AND
更改为 OR
,然后它按预期工作,即使子查询也 return正确的国家 ID
示例数据
TABLE CUST_MERGE
NEW_CUST_REF_ID OLD_CUST_REF_ID
B5000 B6000
B5000 A6000
TABLE CUST_DETAILS
CUST_REF_ID CTRY_ID
B5000 US
B6000 US
A6000 JP
执行CUST_MERGE
中的第一行时,B5000和B6000,return Y,因为ctry_id相同(美国)
CUST_MERGE
执行第二行时,B5000和A6000,returnN,因为ctry_id不同(US,JP)
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
使用此查询获取标志:
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
我有 2 个表:CUST_DETAILS
列 CUST_REF_ID
和
CUST_MERGE
列 NEW_CUST_REF_ID
和 OLD_CUST_REF_ID
,下面的脚本将检查 CUST_MERGE
中的两列是否与 CUST_DETAILS
中的相应列具有相同的国家/地区 ID,并且 return Y,否则 return 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' returned,除非我将条件从 AND
更改为 OR
,然后它按预期工作,即使子查询也 return正确的国家 ID
示例数据
TABLE CUST_MERGE
NEW_CUST_REF_ID OLD_CUST_REF_ID
B5000 B6000
B5000 A6000
TABLE CUST_DETAILS
CUST_REF_ID CTRY_ID
B5000 US
B6000 US
A6000 JP
执行CUST_MERGE
中的第一行时,B5000和B6000,return Y,因为ctry_id相同(美国)
CUST_MERGE
执行第二行时,B5000和A6000,returnN,因为ctry_id不同(US,JP)
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
使用此查询获取标志:
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