Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Display Duplicate Rows Only once

Display Duplicate Rows Only once

Scheduled Pinned Locked Moved Database
databasetutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kodanda Pani
    wrote on last edited by
    #1

    Hi All, I have a table with the below columns. ID , First Name , Last Name , City. The ID column is unique. This table contains duplicate rows (First Name , Last Name , City values are same). Could you please advise me on how to write a select query to display duplicate rows only once along with the other rows. Thanks and Regards Pha

    P 1 Reply Last reply
    0
    • K Kodanda Pani

      Hi All, I have a table with the below columns. ID , First Name , Last Name , City. The ID column is unique. This table contains duplicate rows (First Name , Last Name , City values are same). Could you please advise me on how to write a select query to display duplicate rows only once along with the other rows. Thanks and Regards Pha

      P Offline
      P Offline
      Paddy Boyd
      wrote on last edited by
      #2

      Do you need the ID for display? If not, a simple DISTINCT might be easiest.

      K D 2 Replies Last reply
      0
      • P Paddy Boyd

        Do you need the ID for display? If not, a simple DISTINCT might be easiest.

        K Offline
        K Offline
        Kodanda Pani
        wrote on last edited by
        #3

        Thanks for the response. But i need the ID as well in the display. Regards Pha

        1 Reply Last reply
        0
        • P Paddy Boyd

          Do you need the ID for display? If not, a simple DISTINCT might be easiest.

          D Offline
          D Offline
          David Branscome
          wrote on last edited by
          #4

          -- ALWAYS TEST FIRST -- I don't have your table name -- REPLACE "TABLE" WITH THE ACTUAL TABLE NAME -- Try this to select the dups select count(*),FirstName , LastName , City from "TABLE" group by FirstName , LastName , City having count(*) > 1 -- select the data into another table and TEST THIS -- REPLACE "TABLE" WITH THE ACTUAL TABLE NAME --*********************** DELETE DUPS *************************************** -- the Magic is in the rowcount setting!!! make sure it is set to 1 at the start and 0 on completion set rowcount 1 while exists (select FirstName,LastName,City from "TABLE" group by FirstName , LastName , City having count(*) > 1) begin delete T from "TABLE" T inner join (select FirstName,LastName,City from "TABLE" group by FirstName,LastName,City having count(*) > 1) dup on T.FirstName = dup.FirstName and T.LastName = dup.LastName and T.City = dup.City end go set rowcount 0 go Good Luck DBranscome Phoenix AZ David Branscome

          modified on Tuesday, February 12, 2008 7:19 PM

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups