how to remove redundant records

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2005
Posts: 92
Reputation: michael123 is an unknown quantity at this point 
Solved Threads: 0
michael123 michael123 is offline Offline
Junior Poster in Training

how to remove redundant records

 
0
  #1
Feb 13th, 2009
I have a table in MS SQL DB, the data was recorded with redundancy, is there an easy way to remove all but keep only one, for example:

id   name   age   dept
---------------------------
1    alan      20    A
2    william   23   B
2    william   23   B
3    mike      30   C
3    mike      30   C


I want to trim it to:
id   name   age   dept
---------------------------
1    alan      20    A
2    william   23   B
3    mike      30   C

what is the easy way to do that? DB running on MS SQL 2005.

Any idea?
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: how to remove redundant records

 
0
  #2
Feb 14th, 2009
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3
Reputation: Doron_ is an unknown quantity at this point 
Solved Threads: 0
Doron_ Doron_ is offline Offline
Newbie Poster

Re: how to remove redundant records

 
0
  #3
Feb 15th, 2009
There are few ways to do that, the simple one i think is by adding an index column, and than delete all records which has identical records with smaller index value:

  1. ALTER TABLE MyTable ADD i int IDENTITY;
  2.  
  3. DELETE FROM MyTable
  4. WHERE i IN (
  5. SELECT T1.i
  6. FROM MyTable AS T1
  7. INNER JOIN MyTable AS T2 ON T1.id=T2.id
  8. WHERE T1.i > T2.i
  9. )
  10.  
  11. ALTER TABLE MyTable DROP COLUMN i;


Doron Saar,

Nob Hill Software - tools for database people (+ free stuff!) Nob Hill Software - tools for database people (+ free stuff!)
www.nobhillsoft.com
Last edited by Doron_; Feb 15th, 2009 at 8:31 am. Reason: tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for MS SQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC