By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,649 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,649 IT Pros & Developers. It's quick & easy.

How to get count of multiple criteria

P: 37
I have a database where all companies must meet 3 requirements in order to be in compliance. I need to know out of the total number of companies that are not in compliance how many do not meet requirement 1, how many do not meet requirement 2, and how many do not meet requirement 3. There may be variations - for example, company A meets requirement 1 but not 2 & 3; company B meets requirement 1 & 3 but not 2, etc.

These counts need to be converted into percents based on the total number of companies who are not in compliance.

How can I set this up in a query or report to show this information?

Your help is much appreciated.

Yappy
Dec 1 '10 #1

✓ answered by Oralloy

Yappy,

Bro, you are really need to start learning SQL, if this is the sort of thing you're going to be doing on a regular basis.

Either that, or you're going to have to learn more about reporting in Access, which is an area where we have other experts who are much better than I am.

That said, we can modify your query to include another column, which simply counts companies, like so:

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT 
  2.  (SELECT Count(*) FROM CompanyData WHERE ([CWR Received]=No)) AS [CWR Fail], 
  3.  (SELECT Count(*) FROM CompanyData WHERE ([H&S Approved]=No)) AS [H&S Fail], 
  4.  (SELECT Count(*) FROM CompanyData WHERE ([Liability Approved]=No)) AS [Liability Fail], 
  5.  (SELECT Count(*) FROM CompanyData) AS [Total Companies] 
  6. FROM CompanyData;
or so:

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT 
  2.  (SELECT Count(*) FROM CompanyData WHERE ([CWR Received]=No)) AS [CWR Fail], 
  3.  (SELECT Count(*) FROM CompanyData WHERE ([H&S Approved]=No)) AS [H&S Fail], 
  4.  (SELECT Count(*) FROM CompanyData WHERE ([Liability Approved]=No)) AS [Liability Fail], 
  5.  Count(*) AS [Total Companies] 
  6. FROM CompanyData;
I'm not certain which will work better, performance wise. It shouldn't matter, if the table is small. However, I've seen "it shouldn't matter" go south when small tables become just a few hundred rows, because someone starts really using a feature that had previously been used trivially.

Cheers!

Share this Question
Share on Google+
10 Replies


Oralloy
Expert 100+
P: 983
Yappy,

Use an aggregate queries to get your counts, something like:
Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(*)
  2.   FROM CompanyStatistic
  3.   WHERE (Requirement1 = 'failed');
Then, once you understand how they work, you can use some of the more "advanced" aggregate functions to fold multiple statistics into single queries.

BTW, it helps a lot if you post examples that we can help you modify to meet your requirements. Especially with SQL, as we don't know your table structure or field names.

Luck!
Oralloy
Dec 1 '10 #2

P: 37
Thanks for replying so quickly. I am sure your answer would solve my issue but I don't speak SQL. I only know how to set up basic queries and reports from my tables.

The table contains the following fields:
CompanyName
CWR Received - this is a Yes/No field
H&S Approved - this is a Yes/No field
Liability Approved - this is a True/False field

The company must meet all of the above requirements to be in compliance. I need to set up a query or report that will show me the total number of companies not in compliance & how many do not meet the CWR Received requirement, how many do not meet the H&S Approved requirement, and how many do not meet the Liability Approved requirement.

I hope this helps to clarify.

Thanks,
Yappy
Dec 2 '10 #3

Oralloy
Expert 100+
P: 983
At the risk of sounding cheeky - time to learn SQL...

Try creating a query and placing this in the "SQL View":
Expand|Select|Wrap|Line Numbers
  1. SELECT Count(*) AS Expr1
  2.   FROM CompanyData
  3.   WHERE ([CWR Received]=No)
  4.         OR ([H&S Approved]=No)
  5.         OR ([Liability Approved]=No);
  6.  
Be sure to change the table name "CompanyData" to your table's name.
Dec 2 '10 #4

P: 37
I know I really should learn SQL but have not had the time or resources to do so.

Your SQL statement returns one number when I run the query. Maybe I wasn't specific enough in what I needed.

Let me try again:
CompanyName (need total count not in compliance)
CWR Received - this is a Yes/No field (need count of "No" out of the total number of companies above)
H&S Approved - this is a Yes/No field (need count of "No" out of the total number of companies above)
Liability Approved - this is a True/False field (need count of "No" out of the total number of companies above)
Dec 2 '10 #5

Oralloy
Expert 100+
P: 983
Well, if you don't have a large number of records, this works on my computer running Access....

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT
  2.  (SELECT Count(*) FROM CompanyData WHERE ([CWR Received]=No)) AS [CWR Fail],
  3.  (SELECT Count(*) FROM CompanyData WHERE ([H&S Approved]=No)) AS [H&S Fail],
  4.  (SELECT Count(*) FROM CompanyData WHERE ([Liability Approved]=No)) AS [Liability Fail]
  5. FROM CompanyData;
  6.  
If you can remove the "FROM" clause, even better; however I doubt if you can.

Cheers!
Oralloy
Dec 2 '10 #6

P: 37
Thanks, Oralloy! This works great and we are almost there. I just need to add one more thing. I need the total count of companies that fail included with these other counts. The reason: in order to calculate a percent of the total that fail in each category.
Example:
CWR Fail Count/Total Fail Count = x%
H&S Fail Count/Total Fail Count = x%
Liability Fail Count/Total Fail Count = x%
Dec 3 '10 #7

Oralloy
Expert 100+
P: 983
Yappy,

Bro, you are really need to start learning SQL, if this is the sort of thing you're going to be doing on a regular basis.

Either that, or you're going to have to learn more about reporting in Access, which is an area where we have other experts who are much better than I am.

That said, we can modify your query to include another column, which simply counts companies, like so:

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT 
  2.  (SELECT Count(*) FROM CompanyData WHERE ([CWR Received]=No)) AS [CWR Fail], 
  3.  (SELECT Count(*) FROM CompanyData WHERE ([H&S Approved]=No)) AS [H&S Fail], 
  4.  (SELECT Count(*) FROM CompanyData WHERE ([Liability Approved]=No)) AS [Liability Fail], 
  5.  (SELECT Count(*) FROM CompanyData) AS [Total Companies] 
  6. FROM CompanyData;
or so:

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT 
  2.  (SELECT Count(*) FROM CompanyData WHERE ([CWR Received]=No)) AS [CWR Fail], 
  3.  (SELECT Count(*) FROM CompanyData WHERE ([H&S Approved]=No)) AS [H&S Fail], 
  4.  (SELECT Count(*) FROM CompanyData WHERE ([Liability Approved]=No)) AS [Liability Fail], 
  5.  Count(*) AS [Total Companies] 
  6. FROM CompanyData;
I'm not certain which will work better, performance wise. It shouldn't matter, if the table is small. However, I've seen "it shouldn't matter" go south when small tables become just a few hundred rows, because someone starts really using a feature that had previously been used trivially.

Cheers!
Dec 3 '10 #8

P: 37
(SELECT Count(*) FROM CompanyData) AS [Total Companies] - this gives me the total number of records in the database - 559

This answer gives me the number I need for the total number of companies that failed - 453:

SELECT Count(*) AS Expr1
FROM CompanyData
WHERE ([CWR Received]=No)
OR ([H&S Approved]=No)
OR ([Liability Approved]=No);


My numbers for the category failures are:
CWR Fail = 237
H&S Fail = 418
Liability Fail = 174

So, my percents would be as follows:
CWR Fail 237/Total # failed 453 = 52%
H&S Fail 418/Total # failed 453 = 93%
Liability Fail 174/Total # failed 453 = 38%


The previous response works fine except for needing the total # of companies that failed instead of the total # of records in the database.

Maybe there is someone else out there who can help???

Thank you!
Dec 3 '10 #9

P: 37
I figured out the answer to what I needed. Thank you for all your help. You have actually helped me learn a little bit about SQL.

SELECT Count(*) AS Expr1, (SELECT Count(*) FROM TestTBL2 WHERE ([CWR Sign Off]=No)) AS [CWR Fail], (SELECT Count(*) FROM TestTBL2 WHERE ([Vendor PreQualified by Health/Safety]=No)) AS [H&S Fail], (SELECT Count(*) FROM TestTBL2 WHERE ([Liability Insurance Approved]=No)) AS [Liability Fail]
FROM TestTBL2
WHERE (((TestTBL2.[CWR Sign Off])=No)) OR (((TestTBL2.[Vendor PreQualified by Health/Safety])=No)) OR (((TestTBL2.[Liability Insurance Approved])=No));
Dec 3 '10 #10

Oralloy
Expert 100+
P: 983
Yappy,

Glad I can help.

SQL really is very easy, once you get beyond the syntax. It's just simple set theory, if you have a decent database structure.

Good luck on your project.

Cheers!
Oralloy
Dec 3 '10 #11

Post your reply

Sign in to post your reply or Sign up for a free account.