Solved

Remove duplicate rows from dataTable

Posted on 2010-09-19
10
742 Views
Last Modified: 2013-12-20
Working in ado.net

I have a DataTable called dt
For the sake of simplicity, it has two columns- column called Name and one called x
I would like to remove a row if the value the Name column in one row is the same as a value in the Name column in another row. The x values may be different but that doesnt matter

Please help.....

0
Comment
Question by:sebastiz
10 Comments
 
LVL 5

Accepted Solution

by:
roxviper earned 500 total points
ID: 33711751
0
 
LVL 3

Expert Comment

by:AccessYourBiz_Com
ID: 33711783
create a new table called DeleteNames or call it whatever you want. It should have just one field, "Name", same as your Name table. Make it the primary key and save.
Using the duplicate query wizard, create a query of duplicates from the Name table.
Change this query top an Append Query and append the records to the new table "DeleteNames"
Then create another query and include the the DeleteNames table and the other table that you want to delete the records from. Create a join between the two Name columns.
Change this to a Delete Query. Place the askerick from the table you want to delete from in the query grid.
Run the query it will delete the records. If you have a probke with updatable recordset then you muct go an add an index to the Name field of the table you are deleting from.
0
 
LVL 3

Expert Comment

by:AccessYourBiz_Com
ID: 33712416
I re-read my post and made some typos corrections. See below:

Create a new table called DeleteNames or call it whatever you want. It should have just one field, "Name", same as your Name table. Make it the primary key and save.
Using the duplicate query wizard, create a query of duplicates from the Name table.
Change this query to an Append Query and append the records to the new table "DeleteNames".
Then create another query and include the "DeleteNames" table and the other table that you want to delete the records from. Create a join between the two Name columns of each table.
Change this duplicates query to a Delete Query. Place the asterick from the table you want to delete the records from onto the query grid. Nother else should be in the query grid.
Run the delete query and it will delete the duplicate records. If you get an error that it is not an updatable recordset then you must add an index to the Name field of the table you are deleting from.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 19

Expert Comment

by:Shahan Ayyub
ID: 33717060

Hi!

Try this:
  • Create new Datatable dtUnique.
  • Select distibct records via 'distinct' query. like select distinct [name] from [tablename]
  • Copy records in dtUnique.
  • delete 'dt'
0
 
LVL 19

Expert Comment

by:Shahan Ayyub
ID: 33717067
sorry distinct not distibct.
0
 

Author Comment

by:sebastiz
ID: 33717219
Hi Shahan

But will a distinct query look at the whole row, or can I select DISTINCT on the basis of an item in a named column being the same in two rows?

 
0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 33718189
distinct will give distinct rows on the basis of specified column values

http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataset/thread/ed9c6a6a-a93e-4bf5-a892-d8471b84aa3b
0
 
LVL 19

Expert Comment

by:Shahan Ayyub
ID: 33726834
Hi!

Distinct will give you the records on the basis of columns.

This can give an idea what i want to do:

        SELECT Distinct a.*
        FROM [Practice].[dbo].[products] a

This will select all unique records.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 34049807
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question