Solved

Query with Distinct Count

Posted on 2014-12-10
3
132 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 34

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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

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…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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…

706 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

15 Experts available now in Live!

Get 1:1 Help Now