Update records in same table

Posted on 2007-07-25
Last Modified: 2010-03-20
I have the following records in my temporary  table which I have duplicates I am working: I would like the schoolID match.

tempid        fullname            FirstLastName            schoolid      birthdate         ssn              sourceid
35              John H Smith     John smith                  77777          01/0179      999-99-9999    JXX5
89               John Smith       John Smith                  66666          xxxxxx        xxxxxx            FFF7
 this is just an example but I have a 100 pairs in my  temp table and I am looking for a solution to automate the update.
My objective is to make the schoolid match. In here, I would like update tempID 89 to the same schoolid like tempid 35 -77777.  I would prefer sourceid FFF7 follow and match all schoolIds of sourceid JXX5.


Question by:zachvaldez
    LVL 15

    Expert Comment

    What criteria are you using to consider a record as a duplicate? Just the FirstLastName column?

    Author Comment

    LVL 15

    Accepted Solution

    update      t
    set      schoolid=t3.schoolid,
    from      #tbl t
          join (select firstlastname,min(tempid) tempid from #tbl group by firstlastname) t2 on t.firstlastname=t2.firstlastname
          join (select tempid,schoolid,sourceid from #tbl) t3 on t2.tempid=t3.tempid
    where      t.tempid <> t2.tempid

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
    Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now