473,785 Members | 2,395 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
Sep 27 '06 #1
2 13474
Rich,

For version 2005 is for that a new overloaded DataView.ToTabl e method with a
boolean that tells if it has to be used as distinct.

It is in one of these links
http://windowssdk.msdn.microsoft.com.../wec2b2e6.aspx

For the other versions we have a sample on our website.

http://www.vb-tips.com/dbpages.aspx?Search=distinct

Cor

"Rich" <Ri**@discussio ns.microsoft.co mschreef in bericht
news:26******** *************** ***********@mic rosoft.com...
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

Sep 28 '06 #2

Rich wrote:
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.

Sep 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
8516
by: JochenZ | last post by:
Hello, I have a DataGrid(View) and a DataTable. The DataTable is displayed in the DataGridView: dataGridView.DataSource = theTable; The user is allowed to select some rows and then the following snippet updates some values in the selected rows:
3
32278
by: Rich | last post by:
Hello, I am populating a datagridview from a datatable and filtering the number of rows with a dataview object. Is there a way to retrieve the rows displayed by the datagridview into a separate datatable without having to loop through each column in the datagridview? Or is there a way to retrieve the rows from the original datatable filtered by the dataview into a separate table? I only want to copy the rows from the main table that...
1
3746
by: George | last post by:
Hi, I have a DataGridView, bounded to a DataTable which is populated/updated by a OleDbDataAdapter (with OleDbConnection) to an Access Database. (Sounds familiar to some of you ... I have posted a number of related questions already.) I want to have a worker thread starting to consume data from the DataTable. Imagine that the DataTable contains a list of tasks. From the C# .NET specification, DataTable "is safe for multithreaded...
6
50482
by: George | last post by:
Hi, I have been encountering a refresh problem with DataGridView, which is bound to a DataTable. When I make updates (Add, Delete, update) to the DataGridView, everything flow nicely to DataTable. No problem here. However, when I add data (programatically) to the DataTable, the DataGridView does not refresh right away. If I minimize and show my form,
1
6808
by: martin1 | last post by:
Hi, All, DataGridView is populated with all coumns from database since i need some columns data for condition statement, after that I want to show rest of column in the DataGridView, so how to filter DataGridView column in order to show whatever column i want? Thanks
7
12631
by: Mitchell S. Honnert | last post by:
Is there an equivalent of the DataGrid's DataGridTableStyle for the DataGridView? If not, is there an easy way to duplicate the DataGridTableStyle's functionality for the DataGridView? Here's the background for my question... Before I switched my application over to the Fx 2.0, I used a DataGrid to display my data. I would store different DataGridTableStyles (each one with a custom set of columns) in the DataGrid.TableStyles property...
2
4626
by: Paul | last post by:
Hi all, I have a DataTable and a DataGridView that is using it as a datasource. I'm using the DataTable.Select method and I can find some rows. Is it now possible, somehow to use the result of the Select method (a DataRow collection) to make the DataGridView filtering only on those rows? I mean to display only the found rows and after that to select all previous rows back again? I want to use two buttons on my form "Apply Filter" and...
8
11100
by: cj | last post by:
I have a program to display queries to a SQL db. I type my query in a textbox and click a button and the results display in a datagrid. I could use either dataset or datatable to read the data in then I make the datagrid.datasource = myds.Tables(0) or mydt. Now what I want is to be able to change the query and click the button again and get the new results. I can use clear() to clear the data from a ds/dt but I need the structure gone...
0
2971
by: Proogeren | last post by:
I have made a service that sends a datatable with person information to a client. In the forms init event I call the service to retreive all persons from my service and displays them in a datagridview. In the form I want to be able to filter on e.g. first name. I have used the select method to filter on the datatable but it only returns datarows and not datatable which I need to set as datasource in my datagridview. Also I can not add the...
0
9485
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10161
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8986
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3662
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.