SQl delete problem

Posted on 2009-04-21
Last Modified: 2012-05-06

I posted a question earlier regarding a select problem.

In summary, I needed to get all rows that was duplicated on my table. solution found was:  (see code)
My problem now is,
I need to delete those duplicated rows. but I need to keep one record of each. because If i do a delete, all the duplicate will be gone lets say my table has columns:  tableID, passNumber, lineNo

well if it is:     100     10      1
                      100     10      1

a delete will delete both.    I need to keep one.  is that possible ?? (for all the different duplicates found)
select tableid, passnumber

from yourtable

group by tableid, passnumber

having count(*) > 1

Open in new window

Question by:PhilippeRenaud
    LVL 39

    Expert Comment

    What is your primary key?
    LVL 1

    Author Comment

    It would be TableID and PassNumber.

    my problem is that the client gave me a list with duplicated rows and I created a table with the same columns (without primary keys otherwise it could not insert) and im trying to arrange that Table test correcly so after that I can put the good recrods into the good table..  are you following
    LVL 21

    Accepted Solution

    You could try something like this.


    ;WITH CTE 
    SELECT tableid, passnumber, ROW_NUMBER() OVER (PARTITION BY tableid, passnumber ORDER BY tableid, passnumber) AS ROW 
    FROM yourtable
    WHERE ROW > 1

    Open in new window

    LVL 39

    Expert Comment

    You are going to be stuck with looping since you don't have a key value outside of tableid and passnumber.
    LVL 1

    Author Comment

    How then could I transfer the data to the good table having my primary keys.  otherwise I will have an error of duplicate
    LVL 1

    Author Closing Comment

    worked thank you

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
    In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    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