Solved

Query with Distinct Count

Posted on 2014-12-10
3
137 Views
Last Modified: 2015-01-19
Dear All,

I have a question about using the Count function.  I'm trying to count the number of records in Field 4 for each combination of field1, field 2 and field3.

The input table -

Input Table to Query
The output I'm looking for-

Desired Output
Below is my query it seems to counting the number of records in field 4 and assigning it to field2, instead of breaking it down according to field3.    

SELECT [LocationData].Field1, Count([LocationData]. Field4) AS Field4_Count, MonitoredData.Field2, MonitoredData.Field3
FROM [LocationData] INNER JOIN MonitoredData ON [LocationData].Field2 = MonitoredData.Field2
GROUP BY [LocationData].Field1, MonitoredData.Field2, MonitoredData.Field3
HAVING ((([LocationData].Field1) Is Not Null));

Open in new window


I thought the names would make it easier to explain now I don't think so!

Thanks
0
Comment
Question by:AndyC1000
3 Comments
 
LVL 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 167 total points
ID: 40493279
Maybe you just need to rearrange the field sequence:

SELECT
    [LocationData].Field1,
    MonitoredData.Field2,
    MonitoredData.Field3,
    Count(*) AS Field4_Count

/gustav
0
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 166 total points
ID: 40493544
try this:

select field1, field2, field3, count(1) as cnt
from mytable
group by field1, field2, field3
0
 
LVL 36

Accepted Solution

by:
PatHartman earned 167 total points
ID: 40494075
I don't see anything wrong with the query although, I would use a where clause rather than a having for this particular condition.  The WHERE clause is applied BEFORE any aggregation and the HAVING clause is applied AFTER aggregation so HAVING usually refers to something that was aggregated so you can sum amounts for example and select only the rows that have a net > 0.  Field1 isn't aggregated so it would be the same going in as coming out so get rid of it at the beginning.

Also Count(somefield) doesn't do what you think it does.  Count(somefield) eliminates nulls so
fld1, fld2, fld3
a, 1, x
a, 1, null
a, 1, y

the count would be
a, 1, 2

but if you use Count(*), the count would be
a, 1, 3
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

821 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