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
Solved

Dcount unique

Posted on 2016-11-14
6
43 Views
Last Modified: 2016-11-15
Experts, I have a count in a query but need to count only the unique ID's.  How could I modify the following statement:  
DCount("[ID]","qryOutstanding_2") AS [Count]

thank you

sql:
SELECT qryBal2_Works.FacilityType, qryBal2_Works.Utilized, tblType_dropbox.Type, DCount("[ID]","qryOutstanding_2") AS [Count]
FROM qryBal2_Works INNER JOIN tblType_dropbox ON qryBal2_Works.FacilityType = tblType_dropbox.ID
WHERE (((tblType_dropbox.Type) Like "Working*"));
0
Comment
Question by:pdvsa
6 Comments
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 41887477
quick try:

SELECT qryBal2_Works.FacilityType, qryBal2_Works.Utilized, tblType_dropbox.Type, 
(select count(*) from (select ID from qryOutstanding_2 group by ID)) as  AS [Count]
FROM qryBal2_Works INNER JOIN tblType_dropbox ON qryBal2_Works.FacilityType = tblType_dropbox.ID
WHERE (((tblType_dropbox.Type) Like "Working*"));

Open in new window

0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 41887479
Use Allen Browne's ECount. ECount() offers an extra argument so you can count distinct values. The other arguments are the same as DCount().
0
 
LVL 51

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41887480
typo for my comment, you can try this instead:
SELECT qryBal2_Works.FacilityType, qryBal2_Works.Utilized, tblType_dropbox.Type, 
(select count(*) from (select ID from qryOutstanding_2 group by ID)) AS [Count]
FROM qryBal2_Works INNER JOIN tblType_dropbox ON qryBal2_Works.FacilityType = tblType_dropbox.ID
WHERE (((tblType_dropbox.Type) Like "Working*"));

Open in new window

0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 15

Expert Comment

by:John Tsioumpris
ID: 41887484
The problem lies to qryOutstanding_2...you should have the query to return only unique values and then perform the Dcount....
0
 
LVL 57
ID: 41887533
And just as an aside, you should not be using Domain functions inside a query as you are guaranteeing yourself poor performance (the query optimizer cannot cost it).   Use another query and join to it, or sub-selects as Ryan showed.

 All the Domain functions are nothing more than a SQL statement with a wrapper, so there's no reason not to write it directly in SQL.

 They were only intended to be used in places where SQL was not allowed.

 This is one of the main reasons you hear about Access apps that die after 5 or 6 users.

Jim.
0
 

Author Closing Comment

by:pdvsa
ID: 41887659
Perfect.  thank you.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

860 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