Link to home
Start Free TrialLog in
Avatar of sebastiz
sebastiz

asked on

Remove duplicate rows from dataTable

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.....

ASKER CERTIFIED SOLUTION
Avatar of roxviper
roxviper
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.
Avatar of Shahan Ayyub

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'
sorry distinct not distibct.
Avatar of sebastiz
sebastiz

ASKER

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?

 
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.
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.