473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Counting Records with Criteria

In my database, I am trying to count records from a [Stratum] field that meet the criteria of "[Days}<42". Below is my SQL query code.

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCTROW WARes016.Stratum, Count(([WA Res 016 Final Selection].[Days])) AS CountOfDays
  2. FROM WARes016 INNER JOIN [WA Res 016 Final Selection] ON WARes016.LRS = [WA Res 016 Final Selection].LRS
  3. GROUP BY WARes016.Stratum, [Days]<42
  4. ORDER BY WARes016.Stratum;
This code when run breaks the count into two groups:a count of [Days] per Stratum that meet my criteria and a count of records that don't.

How do I alter the code to only show the count of records that meet my criteria? Or am I going about this all wrong? I am really new at this.

I would appreciate any feedback!

Thank you!
Apr 9 '07 #1
10 2975
Killer42
8,435 Expert 8TB
I'm going to move this to the Access forum where you'll find much more experience in this area. However, I think that the HAVING clause may be what you need to use.
Apr 10 '07 #2
Thanks Killer
Apr 16 '07 #3
ADezii
8,834 Expert 8TB
In my database, I am trying to count records from a [Stratum] field that meet the criteria of "[Days}<42". Below is my SQL query code.

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCTROW WARes016.Stratum, Count(([WA Res 016 Final Selection].[Days])) AS CountOfDays
  2. FROM WARes016 INNER JOIN [WA Res 016 Final Selection] ON WARes016.LRS = [WA Res 016 Final Selection].LRS
  3. GROUP BY WARes016.Stratum, [Days]<42
  4. ORDER BY WARes016.Stratum;
This code when run breaks the count into two groups:a count of [Days] per Stratum that meet my criteria and a count of records that don't.

How do I alter the code to only show the count of records that meet my criteria? Or am I going about this all wrong? I am really new at this.

I would appreciate any feedback!

Thank you!
I assuming that your Days criteria is not applied to the overall Groupings. If it were, you would need a HAVING Clause. Let me know how this works - SQL is not my strong point.
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCTROW WARes016.Stratum, Count(([WA Res 016 Final Selection].[Days])) AS CountOfDays
  2. FROM WARes016 INNER JOIN [WA Res 016 Final Selection] ON WARes016.LRS = [WA Res 016 Final Selection].LRS
  3. WHERE  [Days]<42
  4. GROUP BY WARes016.Stratum HAVING
  5. ORDER BY WARes016.Stratum;
Apr 16 '07 #4
Killer42
8,435 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. ...
  2. GROUP BY WARes016.Stratum HAVING
  3. ORDER BY WARes016.Stratum;
HAVING what???
Apr 17 '07 #5
NeoPa
32,556 Expert Mod 16PB
SQL is a fairly strong area of mine.
You probably don't want to use the DISTINCTROW predicate AS WELL AS the GROUP BY clause. In fact, unless I'm mistaken as to your requirements, the DISTINCTROW predicate will cause your query to give incorrect results under certain (rare) circumstances.
What you are missing is the WHERE clause. Try :
Expand|Select|Wrap|Line Numbers
  1. SELECT WARes016.Stratum, Count(([WA Res 016 Final Selection].[Days])) AS CountOfDays
  2. FROM WARes016 INNER JOIN [WA Res 016 Final Selection] ON WARes016.LRS = [WA Res 016 Final Selection].LRS
  3. WHERE [WA Res 016 Final Selection].[Days]<42
  4. GROUP BY WARes016.Stratum
  5. ORDER BY WARes016.Stratum;
Apr 17 '07 #6
NeoPa
32,556 Expert Mod 16PB
Please let us know if this resolves your issues.
Apr 17 '07 #7
ADezii
8,834 Expert 8TB
HAVING what???
Thanks Killer, just left a 'Dangling HAVING'.
Apr 17 '07 #8
NeoPa - that fixed it and it now counts correctly! Yay! Thank you and everyone else so much for your help!
Apr 17 '07 #9
ADezii
8,834 Expert 8TB
NeoPa - that fixed it and it now counts correctly! Yay! Thank you and everyone else so much for your help!
That NeoPa is something, isn't he?
Apr 17 '07 #10
NeoPa
32,556 Expert Mod 16PB
NeoPa - that fixed it and it now counts correctly! Yay! Thank you and everyone else so much for your help!
That NeoPa is something, isn't he?
I'm pleased that worked for you - Thanks for letting us know :)
Thanks ADezii, coming from a respected peer that means a lot :)
Apr 18 '07 #11

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

Similar topics

8
by: Brenda J. | last post by:
Hello all I'm sure this is a relatively simple query to create, but I can't seem to get my head around it. Here is an example of the two tables I am using: tblAccountTypes ...
13
by: Tony Williams | last post by:
I have a query that I want to count the number of records where the value of a field "txtFactClients" is not 0. I have set up an expression "CountFC: txtFactClients" with Count selected in the...
5
by: chrisc | last post by:
Hello, Hope this is the right place for this... I am creating a testing database for components as they come off a production line. My reports need to select faults that are found, as well...
5
by: ChadDiesel | last post by:
Hello Again, I want to assign a number to each record that will be part of a shipping number. I want the number value to count up until the contract number changes. Then, I want the number to...
1
by: teddyparnell | last post by:
I have a query which selects a number of names from a table, which abide with the criteria. I then want another query to select where the names appear more than three times when the previous query...
1
by: geronimo_me | last post by:
Hi, I have a query that has the following fields: Business: ID Number: LastName: EmpNo: Hours1: Hours2:
1
by: Joseph Berry | last post by:
I have created a report drawing from one table. What I would like to is, grouping by one field, count the number of records within the group that meet two selection criteria (i.e., Field1="A" and...
21
by: jennwilson | last post by:
Ok - So, I am back. I would like to count the number of times a specific record appears in a field from one table in my query and then use that value in the same query to calculate an average....
1
by: RussCRM | last post by:
I need some help getting unique records from our database! I work for a small non-profit homeless shelter. We keep track of guest information as well as what services we have offered for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.