473,320 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Can DataTable's Select or Compute do Count(Distinct())


I have a single DataTable in a DataSet. It has 4 columns and i'd like to
get a handful of counts of unique items in 3 of the 4 columns.
Can a DataTables Select or Compute methods to COUNT DISTINCT?

These two attempts failed
DataRow[] dr = ds.Tables[0].Select( "COUNT(DISTINCT(site_name))" );

object x = ds.Tables[0].Compute( "COUNT(DISTINCT(site_name))",
"ProductionCount > 0" );

The filter in the Compute I don't really want because I'd like to count
distinct on all rows..but the method forces me ot have a filter expression

thanks
mike
Nov 16 '05 #1
2 55806
Nope, but here's how you do it:

http://support.microsoft.com/default...b;EN-US;326176

For your reference, here's the expression syntax
http://msdn.microsoft.com/library/de...ssionTopic.asp
"Michael Howes" <mh****@xblueiguana.ccom> wrote in message
news:ef**************@TK2MSFTNGP10.phx.gbl...

I have a single DataTable in a DataSet. It has 4 columns and i'd like to
get a handful of counts of unique items in 3 of the 4 columns.
Can a DataTables Select or Compute methods to COUNT DISTINCT?

These two attempts failed
DataRow[] dr = ds.Tables[0].Select( "COUNT(DISTINCT(site_name))" );

object x = ds.Tables[0].Compute( "COUNT(DISTINCT(site_name))",
"ProductionCount > 0" );

The filter in the Compute I don't really want because I'd like to count
distinct on all rows..but the method forces me ot have a filter expression

thanks
mike

Nov 16 '05 #2
Michael,
The syntax that ADO.NET supports for Expressions (such as those passed to
Select & Compute) is documented under DataColumn.Expression.

http://msdn.microsoft.com/library/de...ssionTopic.asp

Unfortunately Distinct is not one of the supported functions.

What I've done is create a second table that has primary keys that match the
columns that I want to count, plus a count column. Then for each row in my
primary table I add or update the count in this second table. I use
DataTable.Rows.Find to find the matching row...
The filter in the Compute I don't really want because I'd like to count
distinct on all rows..but the method forces me ot have a filter expression You can pass null for the filter to have it process all rows.

For a good tutorial on ADO.NET as well as a good desk reference once you
know ADO.NET see David Sceppa's book "Microsoft ADO.NET - Core Reference"
from MS press

Hope this helps
Jay
"Michael Howes" <mh****@xblueiguana.ccom> wrote in message
news:ef**************@TK2MSFTNGP10.phx.gbl...
I have a single DataTable in a DataSet. It has 4 columns and i'd like to
get a handful of counts of unique items in 3 of the 4 columns.
Can a DataTables Select or Compute methods to COUNT DISTINCT?

These two attempts failed
DataRow[] dr = ds.Tables[0].Select( "COUNT(DISTINCT(site_name))" );

object x = ds.Tables[0].Compute( "COUNT(DISTINCT(site_name))",
"ProductionCount > 0" );

The filter in the Compute I don't really want because I'd like to count
distinct on all rows..but the method forces me ot have a filter expression

thanks
mike

Nov 16 '05 #3

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

Similar topics

2
by: Jade | last post by:
Hi, I just wanted to ask a quick question regarding datasets. I am creating 3 tables using a dataadapter. what i want to know is that is the relationship created between these datatables...
0
by: Rakesh | last post by:
Hi, I have a datagrid. When the user click on 'Save', I check for the presence for duplicate rows using the datatables.Select method and checking if the length of the array returned is >1. ...
7
by: Luis Esteban Valencia | last post by:
I have this i need to show the datagrid with columns of 2 datatables Dim myadap As New SqlDataAdapter myconn.Open() myadap.TableMappings().Add("Table", "vistaUsuarios")
1
by: skirkby | last post by:
This will be obvious to some - but not me I'm afraid... I am using an SQL data link from my ASP application to a SPROC - this all works fine on standard SELECT statements and JOIN in to a...
9
by: Bob Achgill | last post by:
I would like some example code that shows retrieving and updating selected column data from a DataTable. I am sucessful to load and select the whole row of a DataTable into a datagrid. But...
4
by: sal | last post by:
Greets, All Converting array formula to work with datatables/dataset tia sal I finally completed a formula I was working on, see working code below. I would like to change this code so it...
5
by: Frank | last post by:
Hello All, I am working on a vb.net app where I need to compare to 2 datatables and determine if a string exists in one or both. The first dt is filled from the db. A form is loaded and the...
0
by: StefanPienaar | last post by:
Hi Guys Is there any way in c# (or vb.net) to extract a datatable of data from a dataset with multiple datatables which has relationships set up (containing combined data from the datatables)? ...
2
by: thierry savard | last post by:
I want the 2 compute column to show only 4 decimal, but I cant find a way to affect the return value of 'em. I wrote down this command "compute avg(x)" because I need the average to be in a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.