?
Solved

Distinct Count in Access....

Posted on 2008-06-24
8
Medium Priority
?
771 Views
Last Modified: 2013-11-28
I understand that there is no "Distinct Count" in Access and I can read and understand the logic of most things, but I am not that DB savvy and have been trying to get substrings to work or something to work, but can figure it out.

Here is a sample Table (called Data):

AuditID          SevCode             IP
-----------------------------------------------
10                  Cat I                    x.x.x.12
10                  Cat I                    x.x.x.35
10                  Cat I                    x.x.x.109
10                  Cat I                    x.x.x.198
2109              Cat II                    x.x.x.12
2109              Cat II                    x.x.x.49
2109              Cat II                    x.x.x.124
2109              Cat II                    x.x.x.93
493                Cat III                    x.x.x.35
493                Cat III                    x.x.x.49
493                Cat III                    x.x.x.143
679                Cat I                     x.x.x.93
679                Cat I                     x.x.x.143
4317              Cat III                    x.x.x.93
4317              Cat III                    x.x.x.104

I am trying to count the total number of Cat I's, II's and III's. The code I am using is either counting total distinct records based off of AuditID (this case = 5) or only shows me the the total of one of the Cat's but doesn't distinct them (this case = 6 Cat I).

What I would like to get spit out is:
Cat I   = 2
Cat II  = 1
Cat III = 2

Any ideas?

Thanks in advance!!!
0
Comment
Question by:SMBowen
[X]
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
  • 3
  • 3
  • 2
8 Comments
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 21859115
A distinct count would just give you the result three, as there are three different SevCode values. What you want to do is to group on the SevCode field, and use count to get the number of records in each group:

select SevCode, count(*)
from Data
group by SevCode
order by SevCode
0
 
LVL 1

Author Comment

by:SMBowen
ID: 21859214
@GreenGhost

That gives me an output of:
Cat I = 6
Cat II = 4
Cat III = 5

It counts the total records of each Cat, I would like to know how many distinct AuditID's there are and then break it down by how many of thoses are Cat I's, II's and III's and output that as the final number.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 21859506
select SevCode, count(AuditID & SevCode) from Data group by SevCode order by SevCode
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 21859518
Select SevCode, count(AuditID & SevCode) As ID_SevCoe From Data Group by SevCode Order by SevCode
0
 
LVL 1

Author Comment

by:SMBowen
ID: 21859571
@eghtebas

Same thing, it gives me the total records with Cat I's, II's and III's
0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 400 total points
ID: 21859704
I see. I think that you have to group it twice to get that result.

I can't check the syntax in Access, as I don't have it installed at the moment, but these give the correct result in SQL Server:

select SevCode, count(*)
from (
	select SevCode
	from Data
	group by AuditId, SevCode
) x
group by SevCode
 
or:
 
select SevCode, count(*)
from (
	select distinct AuditId, SevCode
	from Data
) x
group by SevCode

Open in new window

0
 
LVL 1

Author Closing Comment

by:SMBowen
ID: 31470303
Excellent!!!!!! thank you so much!!! They both worked!!!!
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 21860879
sorry... I guess this one should work if you care to try. I know the question is already closed with a good answer from GreenGhost.

Select SevCode, count(AuditID & SevCode) As ID_SevCoe From Data Group by AuditID & SevCode Order by SevCode
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
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…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Suggested Courses

765 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