Find duplicate records

  • Guys hi, one easy question, greatful if you could answer it...

    I have a midlle table that connects users and addresses.

    the table is of the form

    user_fk, address_fk.

    it is higly possible, some users have 2 addresses. That is their user_fk is contained twice (with diferent address_fk) in the table. My question is how to find those users that have 2 addresses (2 address_fk). I know they are 78 in number, but how can i trace the exact records? 🙂

     

    easy one, please answer.. 🙂


    "If you want to get to the top, prepare to kiss alot of bottom"

  • select adress_fk, count(user_fk)

    from middle_table

    group by address_fk

    having count(*) > 1

  • Steve hi, thank you for your help.

    it seems i am doing something wrong.

    after your suggestion i wrote the following

    select

     addr_fk

     ,count(user_fk)

     from

     ecdldb.dbo.UserAddr

    group by addr_fk

    having count(*)>1

    but it returns null records! however i see "duplicate" records (same user id with different addresses ) like the one i show you here, but are not presented in my results... further help pls?

    User_fk Addr_fk

    780579 102261

    780579 102263


    "If you want to get to the top, prepare to kiss alot of bottom"

  • Try this:

    declare @UserAddr table (user_fk int, addr_fk int)

    insert @UserAddr values(780579, 102261)

    insert @UserAddr values(780579, 102263)

    --return just the user_fk

    select user_fk

    from @UserAddr

    group by user_fk

    having count(*)>1

    --return just the user_fk and addr_fk

    select ua.*

    from @UserAddr ua

    inner join (select user_fk

                from @UserAddr

                group by user_fk

                having count(*)>1) dups

    on ua.user_fk = dups.user_fk

  • Thank you both...problem solved. 🙂


    "If you want to get to the top, prepare to kiss alot of bottom"

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply