我試圖從廠商表和vendor_address表找到使用多個字段的數據庫複製的供應商。事情是我做的內心連接越少查詢失去潛在的結果。雖然我在供應商ID中沒有重複,但我希望找到類似的潛在供應商。SQL查找與幾個字段(沒有唯一ID)複製解決
這是到目前爲止我的查詢:
SELECT
o.vendor_id
,o.vndr_name_shrt_user
,O.COUNTRY
,O.VENDOR_NAME_SHORT
,B.POSTAL
,B.ADDRESS1
,SAME_ADDRESS_NB
,SAME_POSTAL_NB
,OC.SAME_SHORT_NAME
,oc.SAME_USER_NUM
FROM VENDOR o
JOIN vendor_addr B ON o.VENDOR_ID = B.VENDOR_ID
INNER JOIN (
SELECT vndr_name_shrt_user, COUNT(*) AS SAME_USER_NUM
FROM VENDOR
WHERE COUNTRY = 'CANADA'
AND VENDOR_STATUS = 'A'
GROUP BY vndr_name_shrt_user
HAVING COUNT(*) > 1
) oc on o.vndr_name_shrt_user = oc.vndr_name_shrt_user
INNER JOIN (SELECT VENDOR_NAME_SHORT, COUNT(*) AS SAME_SHORT_NAME
FROM VENDOR
WHERE COUNTRY = 'CANADA'
AND VENDOR_STATUS = 'A'
GROUP BY VENDOR_NAME_SHORT
HAVING COUNT(*) > 1
) oc on o.VENDOR_NAME_SHORT = oc.VENDOR_NAME_SHORT
INNER JOIN (SELECT POSTAL, COUNT(*) AS SAME_POSTAL_NB
FROM vendor_addr
WHERE COUNTRY = 'CANADA'
AND COUNTRY ='CANADA'
AND POSTAL != ' '
GROUP BY POSTAL
HAVING COUNT(*) > 1
) oc on b.POSTAL = oc.POSTAL
INNER JOIN (SELECT ADDRESS1, COUNT(*) AS SAME_ADDRESS_NB
FROM ps_vendor_addr
WHERE COUNTRY = 'CANADA'
AND COUNTRY ='CANADA'
AND ADDRESS1 != ' '
GROUP BY ADDRESS1
HAVING COUNT(*) > 1
) oc on b.ADDRESS1 = oc.ADDRESS1
WHERE O.COUNTRY ='CANADA'
AND B.COUNTY = 'CANADA';
你爲什麼內側連接?對不希望丟失數據的地方使用左外連接。 – kazzi
請提供[MCVE]包括DDL語句的一些示例數據和這些數據的預期輸出你的表和DML語句。 – MT0
謝謝你,好艱難 – DangerKev