Gladimir Posted June 25, 2003 Posted June 25, 2003 I found this thread on the same subject by Cassio, but the question seems to remain unanswered. I have two fields, MemID and Description, in a table named ReferenceADGroups. The following SQL statement produces a Count of 670: SELECT COUNT(MemID) FROM ReferenceADGroups WHERE (Description="ARMS"); However, some users are duplicated in these groups, so I need to add the DISTINCT function. The following SQL statement produces 665 rows: SELECT DISTINCT MemID FOM ReferenceADGroups WHERE (Description="ARMS"); How do I combine these two functions to write a statement that only produces a COUNT of the DISTINCT Member IDs? Thanks in advance for your help. Quote Never ascribe to malice that which is adequately explained by incompetence. - Napoleon Bonaparte
Mehyar Posted June 25, 2003 Posted June 25, 2003 Do this : SELECT COUNT (DISTINCT MemID) FROM RefernceADGroups WHERE (Description="ARMS") Quote Dream as if you'll live forever, live as if you'll die today
Gladimir Posted June 25, 2003 Author Posted June 25, 2003 Mehyar, thanks for you reply, but that produces an error in MS Access SQL Builder: ERROR: syntax error (missing operator) in query expression 'COUNT (DISTINCT ReferenceADGroups.MemID)' SELECT COUNT (DISTINCT ReferenceADGroups.MemID) AS CountA FROM ReferenceADGroups WHERE ((Not (ReferenceADGroups.MatchCode)="0000") AND ((ReferenceADGroups.Description)="ARMS")); ERROR: undefined function 'DISTINCT' in expression SELECT COUNT (DISTINCT (ReferenceADGroups.MemID)) As CountA FROM ReferenceADGroups WHERE ((Not (ReferenceADGroups.MatchCode)="0000") AND ((ReferenceADGroups.Description)="ARMS")); I know we have to be close to something that works. Thanks for the help. Quote Never ascribe to malice that which is adequately explained by incompetence. - Napoleon Bonaparte
JABE Posted June 25, 2003 Posted June 25, 2003 It's pretty ugly but try this: SELECT COUNT(*) FROM (SELECT MemID FROM ReferenceADGroups WHERE (Description="ARMS") GROUP BY MemID) AS MyCount; Quote
Mehyar Posted June 25, 2003 Posted June 25, 2003 Gladimir, what i am sure of is that it works in SQL Server cause i tried it before replying to you but i dont know about MS Access i will try to look into it Quote Dream as if you'll live forever, live as if you'll die today
JABE Posted June 25, 2003 Posted June 25, 2003 SELECT COUNT(DISTINCT fieldname) ... is valid in SQL Server but not in MS Access :( Quote
JABE Posted June 25, 2003 Posted June 25, 2003 Btw, alternatively you can do: SELECT COUNT(*) FROM (SELECT DISTINCT MemID FROM ReferenceADGroups WHERE (Description="ARMS") ) AS MyCount; Quote
Mehyar Posted June 25, 2003 Posted June 25, 2003 Eureka !!! Try this ( i tried it in Access and it worked) Select Count (MemId) From (Select Distinct MemId from RefernceADGroups WHERE Description="ARMS") Quote Dream as if you'll live forever, live as if you'll die today
Gladimir Posted June 25, 2003 Author Posted June 25, 2003 SOLUTION! That is beautiful Mehyar. It is working perfectly in the Access Query Builder and here it is in my code: string strScalar = "SELECT COUNT(MemID) AS CountA " + "FROM (SELECT DISTINCT MemID FROM ReferenceADGroups " + "WHERE (Description='" + strDescription + "') " + "AND NOT(MatchCode) = '" + strMatchCode + "')"; Thanks for you help! Quote Never ascribe to malice that which is adequately explained by incompetence. - Napoleon Bonaparte
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.