Solved

How to Count Unique using WHERE clause MS Access query

Posted on 2009-04-06
6
482 Views
Last Modified: 2013-11-29
I am having trouble  figuring out how to get a query to return the count of unique account numbers in a particular table in my db.

The query first links two tables, 'time_code_ref_tbl' to 'SumEarnings_tbl' on field 'cycle_date'.  The purpose of this is to use a user defined 'time_code' as specified in a textbox on the main form in the db for the query criteria.

For example when '0209' is entered in the form's text box, I would like the query to return the count of unique account numbers for the two cycledates that correspond to 0209 as according to the 'time_code_ref_tbl'.  Here is some SQL I am statring with.

Any suggestions?

(It doesn't seem like putting in 'DISTINCT' does anything)

SELECT DISTINCT Count(SumEarnings2009.GroupAccount) AS CountOfGroupAccount
FROM SumEarnings2009 INNER JOIN TIME_CODE_REFERENCE_TABLE ON SumEarnings2009.CycleDate=TIME_CODE_REFERENCE_TABLE.fldTOP_CYCLE_DATE
WHERE (((TIME_CODE_REFERENCE_TABLE.fldTIME_CODE)=Forms![GEM to CMR ENTER DIFFERENCES FORM]!Time_Code_txt));
0
Comment
Question by:pgerman
[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
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:gnovakhs2n
ID: 24078204
try like this:

select count(x.GroupAccount) as CountOfGroupAccount from (
SELECT DISTINCT SumEarnings2009.GroupAccount AS GroupAccount
FROM SumEarnings2009 INNER JOIN TIME_CODE_REFERENCE_TABLE ON SumEarnings2009.CycleDate=TIME_CODE_REFERENCE_TABLE.fldTOP_CYCLE_DATE
WHERE (((TIME_CODE_REFERENCE_TABLE.fldTIME_CODE)=Forms![GEM to CMR ENTER DIFFERENCES FORM]!Time_Code_txt)) as x
0
 

Author Comment

by:pgerman
ID: 24078370
I still can't get that to work, but I think I am just going to simplify things and break it up into two queries.  One that will return a unique list, the other to count from that query.

The end result will be the same to the user.

Thank you anyway for the quick response.

0
 
LVL 2

Expert Comment

by:gnovakhs2n
ID: 24078399
thats exactly what this query does, probably in MS-Access Syntax you have to remove the "as" from "as x" at the end
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:pgerman
ID: 24078575
Its still giving me a 'Syntax error in the from clause'  
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 24078773
I know this sounds too easy but, have you tried working in the non-SQL Design View of the Query Wizard?  
Create the query in the Query Wizard without trying to get the DISTINCT working (in other words, let it return all of the multpile instances).  Then right click to viw the properties of the query (or switch to the SQL view) and add the DISTINCT clause.
I understand that "real developers work with SQL and not GUI" but that's right up there with "real men don't cry" and "real men never, ever wear pink shirts". ;-)
0
 
LVL 2

Accepted Solution

by:
gnovakhs2n earned 500 total points
ID: 24078783
Oh, i see

a bracket problem ... try the snippet
select count(x.GroupAccount) as CountOfGroupAccount from 
(
SELECT DISTINCT SumEarnings2009.GroupAccount AS GroupAccount
FROM SumEarnings2009 INNER JOIN TIME_CODE_REFERENCE_TABLE ON SumEarnings2009.CycleDate=TIME_CODE_REFERENCE_TABLE.fldTOP_CYCLE_DATE
WHERE 
(((TIME_CODE_REFERENCE_TABLE.fldTIME_CODE)=Forms![GEM to CMR ENTER DIFFERENCES FORM]!Time_Code_txt))
)  x 

Open in new window

0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
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 …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

732 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