sql server - SQL find records with duplicate email and date of birth -


i'm trying write query find duplicate records in database. want find records (not count) emailaddress , dateofbirth (both columns) exist on record.

account tbl contains emailaddress.

user tbl contains dateofbirth

join on accountid

the following query selects records emailaddress exists in record or dateofbirth exists in record, i'm unable combine 2 conditions. if i'm correct far, 'and' on line 7 acts more 'or' in case..?

select a.accountname, a.emailaddress, u.dateofbirth account join [user] u on a.accountid = u.accountid a.emailaddress in ( select emailaddress account group emailaddress having count(*) > 1 ) ,        dateofbirth in( select dateofbirth [user] group dateofbirth having count(*) > 1 ) order u.dateofbirth, a.emailaddress 

for example, may produce 50 records. if through them, find 5 records matching emailaddress, 4 of them have same dateofbirth. 5th record displaying due record in database same dateofbirth different emailaddress.

i'd find records have both matching email , dob.

thanks always, please ask if require further description.

regards json

using approach, can use exists:

select a.accountname, a.emailaddress, u.dateofbirth account join      [user] u      on a.accountid = u.accountid exists (select emailaddress               account a2 join                    [user] u2                    on a.accountid = u.accountid               a2.emailaddress = a.emailaddress ,                     u2.dateofbirth = u.dateofbirth               group emailaddress               having count(*) > 1              ) order u.dateofbirth, a.emailaddress; 

a better way use window/analytic functions:

select accountname, emailaddress, dateofbirth (select a.accountname, a.emailaddress, u.dateofbirth,              count(*) on (partition a.emailaddress, u.dateofbirth) cnt       account join            [user] u            on a.accountid = u.accountid      ) ua cnt > 1 order dateofbirth, emailaddress; 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -