Solved

Count number of unique values from a query

Posted on 2015-01-30
17
156 Views
Last Modified: 2015-01-31
I have a report that has an unbound field.  The report's record source is a query.  In that queries detail is a field named PoN.

In that report field I want to show the number of unique PoN's there are in the query.

So something like this which of course isn't right:

= DCount UNIQUE ("[PoN]", "qryTestQuery")
0
Comment
Question by:SteveL13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
  • 2
  • +1
17 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40580348
to get a count of unique PoN

SELECT Count(A.PoN) AS CountOfPoN
FROM (SELECT PoN
FROM YourQuery
GROUP BY PoN
) AS A;
0
 

Author Comment

by:SteveL13
ID: 40580354
But how do I get that in the field's value on the report?  VBA code of some kind?
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40580363
save the query as qryPoNCount   or whatever name you want

then just do a lookup

=dlookup("CountOfPoN", "qryPoNCount")
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 13

Expert Comment

by:Jeff Darling
ID: 40580372
This will list the unique list of PoN with the count of each.

select PoN,count(PoN) as cnt from mytable
group by PoN

Open in new window

0
 

Author Comment

by:SteveL13
ID: 40580742
Now I just found out the field has to be in a group header.  ?????
0
 
LVL 37

Expert Comment

by:PatHartman
ID: 40580782
If the field is a group header, you can just add a control in the corresponding group footer:

=count(*)

And that will give you a distinct count.
0
 

Author Comment

by:SteveL13
ID: 40581414
I probably didn't explain well enough.  The report has a group.  In the report's group footer I have a field that needs to show the number of unique (or is it distinct) values of PoN in that group.

So if in the detail section we have:

Baseball
Football
Basketball
Baseball

then the field needs to show 3.
0
 
LVL 37

Expert Comment

by:PatHartman
ID: 40581491
Access SQL doesn't have a specific method to do this as SQL Server and other RDBMS' do so you have to improvise.  You need a query that groups and counts the recordset used to populate the subreport and it needs to be filtered the same way.

Did you try the query posted by Rey?
0
 

Author Comment

by:SteveL13
ID: 40581537
I'm trying this:  but I get a syntax error on 'Count(A.PPo_#) '

(note the field is really PPo_#, not PoN as originally posted)

SELECT Count(A.PPo_#) AS CountOfPPo_#
 FROM (SELECT PPo_#
 FROM qry_byOperator - Draw Machine
 GROUP BY PPo_#
 ) AS A;
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40581550
that is the problem when names are posted correctly


SELECT Count(A.[PPo_#]) AS CountOfPPo_#
  FROM (SELECT [PPo_#]
  FROM [qry_byOperator - Draw Machine]
  GROUP BY [PPo_#]
  ) AS A;
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40581551
that is the problem when names are NOT posted correctly
0
 

Author Comment

by:SteveL13
ID: 40581565
Sorry.  Now when I use this I get "The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect"

SELECT Count(A.[PPo_#]) AS CountOfPPo_#
   FROM (SELECT [PPo_#]
   FROM [qry_byOperator - Draw Machine]
   GROUP BY [PPo_#]
   ) AS A;
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 40581584
SELECT Count(A.[PPo_#]) AS [CountOfPPo_#]
    FROM (SELECT [PPo_#]
    FROM [qry_byOperator - Draw Machine]
    GROUP BY [PPo_#]
    ) AS A;
0
 

Author Comment

by:SteveL13
ID: 40581602
Status:

I have created a new query named qryUniquePONcounts.  The SQL for the new query is:

SELECT Count(A.[PPo_#]) AS [CountOfPPo_#]
FROM (SELECT [PPo_#] FROM [qry_byOperator - Draw Machine] GROUP BY [PPo_#])  AS A;

The in the footer section of the report I have an unbound field with this as the control source:

=DLookUp("CountOfPPo_#","qryUniquePONcounts")

When I run the report I get #Error in the field.

???
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40581603
=DLookUp("[CountOfPPo_#]","qryUniquePONcounts")
0
 

Author Comment

by:SteveL13
ID: 40581606
Getting close I think.  But the field is displaying the total not the group total.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40581612
I think the original  question that you posted has been answered.

post another q and give a detail information
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question