Solved

HELP With Access Query

Posted on 2013-06-20
6
168 Views
Last Modified: 2013-11-27
I am trying to create the following query in access 2010

SELECT COUNT (DISTINCT C.FullName) AS Total, C.Owner
FROM (Contact AS C INNER JOIN xmoWizardCancellation AS WC ON C.RecId = WC.xfContactRecID) INNER JOIN Activity AS A ON C.RecId = A.ParentLink_RecID
WHERE WC.xfStatus = 'Can'
AND WC.CreatedDateTime > 2013-06-18
AND (C.Owner = 'owner1'
OR C.Owner = 'owner2'
OR C.Owner = 'owner3')
GROUP By C.Owner

but keep getting the following error:
"Syntax Error (Missing Operator) in query expression 'COUNT (DISTINCT C.FullName)'

Any help will be appreciated
0
Comment
Question by:ahmzb1990
6 Comments
 
LVL 12

Expert Comment

by:pdebaets
Comment Utility
I'm not sure that the "DISTINCT" and "GROUP BY" go together. Try

Select Count(C.FullName) as Total, ...
0
 
LVL 5

Expert Comment

by:DOSLover
Comment Utility
You won't be able to do 'Count' on 'Distinct'. The Select needs to be changed as:
SELECT COUNT (C.FullName) AS Total, C.Owner
....
.....
0
 

Author Comment

by:ahmzb1990
Comment Utility
Thanks for your response guy however, without distinct it gives me duplicate values. Any other way of doing this?
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 12

Expert Comment

by:pdebaets
Comment Utility
Not quite sure what you're after, but you may want to try

Select C.FullName, Count(*) as Total
From ...
Where ...
Group by C.FullName;
0
 
LVL 5

Accepted Solution

by:
DOSLover earned 500 total points
Comment Utility
Hi ahmzb1990,
Unlike SQL Server or Oracle, MS Access doesn't support DISTINCT with a Count. There is however a workaround. You need to get count in a subquery and then join that subquery with the main. Here is a helpful link: http://blogs.office.com/b/microsoft-access/archive/2007/09/19/writing-a-count-distinct-query-in-access.aspx

: SELECT DISTINCT C2.FullNameCount as FullNameCount, C.Owner
 FROM ((Contact as C 
             INNER JOIN xmoWizardCancellation as WC ON C.RecId = Wc.cfContactRecId)
             INNER JOIN Activity as A ON A.ParentLink_RecId=C.RecId)
             INNER JOIN
             (SELECT C1.Owner, Count(C1.FullName) as FullNameCount
                  FROM (SELECT Distinct Owner, FullName from Contact) as C1
                 GROUP BY Owner) as C2 ON C2.Owner = C.Owner
Where  WC.xfStatus = 'Can'
AND WC.CreatedDateTime > 2013-06-18
AND (C.Owner = 'owner1'
OR C.Owner = 'owner2'
OR C.Owner = 'owner3')

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
1. date/time literals need to be # delimited.
2. since this is Access, use mm/dd/yyyy formatted date literals
Example
AND WC.CreatedDateTime > #06/18/2013#

Open in new window

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now