Solved

Remove duplicate rows from dataTable

Posted on 2010-09-19
10
743 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

792 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