Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / May 2004

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michael Howes - 06 May 2004 22:57 GMT
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
William Ryan  eMVP - 07 May 2004 00:43 GMT
Nope, but here's how you do it:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;326176

For your reference, here's the expression syntax
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fSystemDataDataColumnClassExpressionTopic.asp


>  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.
[quoted text clipped - 11 lines]
> thanks
> mike
Jay B. Harlow [MVP - Outlook] - 07 May 2004 00:53 GMT
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/default.asp?url=/library/en-us/cpref/html/frlr
fSystemDataDataColumnClassExpressionTopic.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

>  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.
[quoted text clipped - 11 lines]
> thanks
> mike

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.