Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2003
    Posts
    18

    Unanswered: Fetching Duplicate Rows

    Hi,

    I have some duplicate rows in a table. I didnt define any primary key or unique key on the table.

    I can get unique rows using DISTINCT, but i want to fetch only the duplicated rows and also i want to delete the duplicated rows.

    How can i do it?

    Please help me.....

    Thanx in Advance

  2. #2
    Join Date
    Jul 2003
    Location
    The Dark Planet
    Posts
    1,401
    I have some duplicate rows in a table. I didnt define any primary key or unique key on the table.
    Can you paste some DDL. Are all the columns for the duplicated rows same ? Is there any date time stamp ??? You could find some help Here
    Last edited by Enigma; 01-14-04 at 03:43.
    Get yourself a copy of the The Holy Book

    order has no physical Brett in The meaning of a Kaiser . -database data

  3. #3
    Join Date
    Jan 2004
    Location
    Romania - Bucharest
    Posts
    50

    Re: Fetching Duplicate Rows

    Try something like:

    Select * from Table
    Where {Key Fields} IN
    (select {key Fields} from Table
    group by {Key Fields}
    Having Count(*) >1)

    Where Table is your Table/Query and {Key Fields} is the list of fields you want to search for duplicates on

    HTH

    Marp

    [
    QUOTE]Originally posted by d_kishan
    Hi,

    I have some duplicate rows in a table. I didnt define any primary key or unique key on the table.

    I can get unique rows using DISTINCT, but i want to fetch only the duplicated rows and also i want to delete the duplicated rows.

    How can i do it?

    Please help me.....

    Thanx in Advance
    [/QUOTE]

  4. #4
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    You say you want to delete the duplicated rows, but I bet what you really want is to delete all but one of each duplicated row. Your first step is to add a primary key. Otherwise, there is no way to discern which of the duplicated rows to retain and your only option will be to select DISTINCT into a new table and then replace your old table.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  5. #5
    Join Date
    Jan 2004
    Location
    Canada
    Posts
    2

    Re: Fetching Duplicate Rows

    If you want to know how many duplicate rows you have per your Key criteria,

    do this

    [Select Key1,Key2,..Keyn, count(*)
    from Table
    group by {Key1,Key2,...Keyn}
    Having Count(*) >1)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •