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. sql query - eliminate all duplicates but one

sql query - eliminate all duplicates but one

Scheduled Pinned Locked Moved Database
databasehelptutorial
2 Posts 2 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.
  • H Offline
    H Offline
    Hanzaplast
    wrote on last edited by
    #1

    hi guys.... i have this table ID_____NAME______OTHER_____ 1_____Name 1__First value 2_____Name 2__Second value 3_____Name 2__Second value 4_____Name 2__Second value 5_____Name 2__Second value 6_____Name 3__Third value 7_____Name 3__Third value 8_____Name 4__Fourth value i need to make SQL query wich will eliminate all duplicates from table (all fields are comparing, except id) but one. for example, after query is executed table should look like this: ID_____NAME______OTHER_____ 1_____Name 1__First value 2_____Name 2__Second value 6_____Name 3__Third value 8_____Name 4__Fourth value any help appreciated...

    S 1 Reply Last reply
    0
    • H Hanzaplast

      hi guys.... i have this table ID_____NAME______OTHER_____ 1_____Name 1__First value 2_____Name 2__Second value 3_____Name 2__Second value 4_____Name 2__Second value 5_____Name 2__Second value 6_____Name 3__Third value 7_____Name 3__Third value 8_____Name 4__Fourth value i need to make SQL query wich will eliminate all duplicates from table (all fields are comparing, except id) but one. for example, after query is executed table should look like this: ID_____NAME______OTHER_____ 1_____Name 1__First value 2_____Name 2__Second value 6_____Name 3__Third value 8_____Name 4__Fourth value any help appreciated...

      S Offline
      S Offline
      SeMartens
      wrote on last edited by
      #2

      Are you using Microsoft SQL Server? If yes, you could use the following query (you have to adopt it to your db):

      ;WITH MyTable_CTE(Name, Ranking)
      AS
      (
      SELECT Name,Ranking = DENSE_RANK() OVER (PARTITION BY Name ORDER BY NEWID() ASC)
      FROM MyTable
      )
      DELETE FROM MyTable_CTE
      WHERE Ranking > 1

      Hope this helps Regards Sebastian

      It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

      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