Solved

Distinct Count in Access....

Posted on 2008-06-24
8
767 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Help 27 45
Run SQL Server Proc from Access 11 31
Access Open Report with SQL Parameter 11 30
Change date stored as "short text" YYYYMMDD to MM/DD/YYYY? 27 28
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
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…

777 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