943,633 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 1485
  • MS SQL RSS
Feb 13th, 2009
0

how to remove redundant records

Expand Post »
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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
michael123 is offline Offline
93 posts
since Jun 2005
Feb 14th, 2009
0

Re: how to remove redundant records

Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Feb 15th, 2009
0

Re: how to remove redundant records

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:

MS SQL Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Doron_ is offline Offline
3 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: MS SQL 2005 Trigger : Transaction Scope and Parameters
Next Thread in MS SQL Forum Timeline: create update trigger with parameters mssql





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC