如何使SQL查询输出多次出现的记录并在结果中包含重复记录?

时间:2015-07-29 12:30:01

标签: sql sql-server sql-server-2000

我是一名新的SQL程序员,我一直在试图弄清楚如何编写一个SQL查询,该查询显示地址在4个单独的属性中是否重复并输出所有重复记录。我编写了以下查询,但它似乎只显示其中一个重复记录,但不是全部。

SELECT 
apvenfil_sql.vend_no,
apvenfil_sql.vend_name,
apvenfil_sql.sort_name,
apvenfil_sql.addr_1,
apvenfil_sql.addr_2,
apvenfil_sql.city,
apvenfil_sql.state,
apvenfil_sql.zip,
apvenfil_sql.country,
apvenfil_sql.contact,
apvenfil_sql.acct_dt,
apvenfil_sql.last_active_dt,
apvenfil_sql.ap_terms_cd,
apvenfil_sql.cus_no,
apvenfil_sql.purch_ytd,
apvenfil_sql.purch_last_yr,
apvenfil_sql.amt_pd_ytd,
apvenfil_sql.amt_pd_last_yr,
apvenfil_sql.balance,
apvenfil_sql.lst_chk_dt,
apvenfil_sql.last_chk_amt,
apvenfil_sql.chk_no,
apvenfil_sql.payee_name,
apvenfil_sql.A4GLIdentity,
apaltadr_sql.vend_no,
apaltadr_sql.vend_alt_adr_cd,
apaltadr_sql.vend_name,
apaltadr_sql.addr_1,
apaltadr_sql.addr_2,
apaltadr_sql.city,
apaltadr_sql.state,
apaltadr_sql.zip,
apaltadr_sql.country,
apaltadr_sql.A4GLIdentity
FROM
apvenfil_sql
LEFT JOIN apaltadr_sql
    ON apaltadr_sql.vend_no = apvenfil_sql.vend_no 
WHERE 
apvenfil_sql.addr_1 IN
    (     SELECT addr_1 
          FROM apvenfil_sql
          GROUP BY addr_1
         HAVING COUNT(*) > 1
    ) OR
apvenfil_sql.addr_2 IN
    (     SELECT addr_2 
          FROM apvenfil_sql
          GROUP BY addr_2
          HAVING COUNT(*) > 1
    ) OR
apaltadr_sql.addr_1 IN 
    (     SELECT addr_1 
          FROM apaltadr_sql
          GROUP BY addr_1
          HAVING COUNT(*) > 1
    ) OR
apaltadr_sql.addr_2 IN 
    (     SELECT addr_2 
          FROM apaltadr_sql
          GROUP BY addr_2
          HAVING COUNT(*) > 1
    ) 
ORDER BY 
apvenfil_sql.vend_no;

作为参考,SQL Server版本是2000。

非常感谢你的帮助!

0 个答案:

没有答案