Solved

Distinct Count in Access....

Posted on 2008-06-24
8
761 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
  • 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 33

Expert Comment

by:Mike Eghtebas
ID: 21859506
select SevCode, count(AuditID & SevCode) from Data group by SevCode order by SevCode
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 21859518
Select SevCode, count(AuditID & SevCode) As ID_SevCoe From Data Group by SevCode Order by SevCode
0
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.

 
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 100 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 33

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
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…

758 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

21 Experts available now in Live!

Get 1:1 Help Now