filter dataTable/datagridview to show top 1 row for duplicate IDs?

G

Guest

Hello,

I am just checking if there is a property or technique for displaying or
retrieving from a dataTable the top 1 row(s) for rows containing duplicate
keys (IDs). I have to pull data from a sql server DB. The resultset
contains 10 distinct rows but the keys are duplicate - like record 12345 is
listed twice but distinct because some of the other data for each field is
different. I only need to see one of the 2 rows for ID 12345 (and for ID
32234...). I tried a select statement like "Select Top 1 * From (Select
.....Where t1.ID = t2.ID...). A query like this works OK for small source
data, but my source tables contain hundreds of thousands of rows - so the
query just kept running without returning any data. If I do a straight
forward "Select ....) without the subquery I get my 10 rows in .5 seconds.

So before I write a bunch of spaghetti code to retrieve the top 1 row from
my result data table for each duplicate ID, is there a property/technique to
do this? Is it possible to write a sql type Select statement against a
VB.Net dataTable?

My spaghetti code will be something like I will store each ID in an array.
I will loop through the array and pull only one row for that ID into another
dataTable until I have my 5 Top 1 rows of the original 10. Is this my only
option or is there a better way/method?

Thanks,
Rich
 
B

Brian Tkatch

Rich said:
Hello,

I am just checking if there is a property or technique for displaying or
retrieving from a dataTable the top 1 row(s) for rows containing duplicate
keys (IDs). I have to pull data from a sql server DB. The resultset
contains 10 distinct rows but the keys are duplicate - like record 12345 is
listed twice but distinct because some of the other data for each field is
different. I only need to see one of the 2 rows for ID 12345 (and for ID
32234...). I tried a select statement like "Select Top 1 * From (Select
....Where t1.ID = t2.ID...). A query like this works OK for small source
data, but my source tables contain hundreds of thousands of rows - so the
query just kept running without returning any data. If I do a straight
forward "Select ....) without the subquery I get my 10 rows in .5 seconds.

So before I write a bunch of spaghetti code to retrieve the top 1 row from
my result data table for each duplicate ID, is there a property/technique to
do this? Is it possible to write a sql type Select statement against a
VB.Net dataTable?

My spaghetti code will be something like I will store each ID in an array.
I will loop through the array and pull only one row for that ID into another
dataTable until I have my 5 Top 1 rows of the original 10. Is this my only
option or is there a better way/method?

Thanks,
Rich

In SQL server can't you just:

SELECT * FROM table GROUP BY key?

B.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top