PC Review


Reply
Thread Tools Rate Thread

Counting distinct values

 
 
=?Utf-8?B?SUx1dkFjY2Vzcw==?=
Guest
Posts: n/a
 
      23rd Feb 2006
Hi-
I am attempting to create a summary query where I can provide a count of how
many records exist per category (Grouped by Category), but I also need to
provide a count of how many unique values are in a particular field. I would
like these summarized fields in the query to exist side by side (right next
to each other in the same query). Is there a specific function I can utilize
in Access that will aggregate based on a unige value?
 
Reply With Quote
 
 
 
 
=?Utf-8?B?c2NoYXN0ZWVu?=
Guest
Posts: n/a
 
      23rd Feb 2006
Some data examples and the table description would help. Sounds like you are
needing:
Select count([YourTbl].[SomeField] as CountOfField
From YourTbl
Group By YourTbl.Catagory

But I am taking a wild guess without some exaples of what you have and what
you want. Maybe somebody else will have a better solution

"ILuvAccess" wrote:

> Hi-
> I am attempting to create a summary query where I can provide a count of how
> many records exist per category (Grouped by Category), but I also need to
> provide a count of how many unique values are in a particular field. I would
> like these summarized fields in the query to exist side by side (right next
> to each other in the same query). Is there a specific function I can utilize
> in Access that will aggregate based on a unige value?

 
Reply With Quote
 
James A. Fortune
Guest
Posts: n/a
 
      24th Feb 2006
ILuvAccess wrote:
> Hi-
> I am attempting to create a summary query where I can provide a count of how
> many records exist per category (Grouped by Category), but I also need to
> provide a count of how many unique values are in a particular field. I would
> like these summarized fields in the query to exist side by side (right next
> to each other in the same query). Is there a specific function I can utilize
> in Access that will aggregate based on a unige value?


This is a very interesting problem. Here is how I went about it:

tblGroupedData
RID Auto
GroupID Long
Data Text

RID GroupID Data
1 1 A
2 1 Null
3 1 Null
4 2 D
5 2 E
6 2 F

qryCountDistinct:
SELECT GroupID, (SELECT COUNT(*) FROM tblGroupedData AS A WHERE
Nz(A.Data, '') IN (SELECT Nz(B.Data, '') FROM tblGroupedData AS B WHERE
B.GroupID = tblGroupedData.GroupID AND B.RID > A.RID)) AS
CountDuplicated, Count(*) AS GroupCount, GroupCount - CountDuplicated AS
CountDistinct FROM tblGroupedData GROUP BY GroupID;

!qryCountDistinct:
GroupID CountDuplicated GroupCount CountDistinct
1 1 3 2
2 0 3 3

With different data:

RID GroupID Data
1 1 A
2 1 Null
3 1 Null
4 1 Null
5 2 D
6 2 D
7 2 F

!qryCountDistinct:
GroupID CountDuplicated GroupCount CountDistinct
1 2 4 2
2 1 3 2

With another set of data:

RID GroupID Data
1 1 A
2 1 A
3 1 B
4 1 B
5 2 Null
6 2 Null
7 2 F

!qryCountDistinct:
GroupID CountDuplicated GroupCount CountDistinct
1 2 4 2
2 1 3 2

Then the final version:

qryCountDistinct:
SELECT GroupID, GroupCount-(SELECT COUNT(*) FROM tblGroupedData AS A
WHERE Nz(A.Data, '') IN (SELECT Nz(B.Data, '') FROM tblGroupedData AS B
WHERE B.GroupID = tblGroupedData.GroupID AND B.RID > A.RID)) AS
CountDistinct, Count(*) AS GroupCount FROM tblGroupedData GROUP BY GroupID;

!qryCountDistinct:
GroupID CountDistinct GroupCount
1 2 4
2 2 3

It seems to work. Try it out and let us know. It may be possible to
improve it once I figure out how it works :-).

James A. Fortune
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Counting Distinct Values Johnny Microsoft Excel Discussion 4 25th Apr 2010 12:26 PM
Counting Distinct Values Access::Student Microsoft Access 1 28th May 2009 09:52 PM
Counting distinct? Mark Microsoft Access Queries 2 29th Jan 2009 07:05 PM
Counting Distinct Values giantwolf Microsoft Excel Misc 4 29th Dec 2005 03:03 PM
Counting distinct Rita Palazzi Microsoft Excel Misc 2 1st Nov 2004 04:00 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:09 PM.