473,625 Members | 3,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query counting records for pre-defined ranges

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

AccountTypeID AccountType AccountNumberRa nge
1 Bronze between 10000 and 19999
2 Silver between 20000 and 29999
3 Gold between 30000 and 39999
4 Platinum between 40000 and 49999
5 Diamond betwee 50000 and 59999
6 Executive between 80000 and 89999
7 Family between 90000 and 99999
8 Other between 60000 and 79999 or > 99999

tblMembers

AccountNum Surname Given Name
47589 Jackson Helen
12678 Hayward Alice
97166 Bond Dee
97904 Samuel Michael
27184 Anderson Louis
16041 Janes Hanora
57909 Williams Carla
etc.

I want to count all the members for each account type. Their account
number determines what type of member they are ie. a member with the
account number 35111 would be a Gold member.

The result I am looking for would like like this:

Bronze 242
Silver 32
Gold 332
Platinum 422
Diamond 11
Executive 21
Family 255
Other 71
I can't seem to think of how to structure a query that would use the
AccountNumberRa nge to group the records to count them.

Any help would be greatly appreciated.

Brenda J.

Nov 13 '05 #1
8 1735
"Brenda J." <no**********@y ahoo.com> wrote in message
news:2t******** *************** *********@4ax.c om...
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

AccountTypeID AccountType AccountNumberRa nge
1 Bronze between 10000 and 19999
2 Silver between 20000 and 29999
3 Gold between 30000 and 39999
4 Platinum between 40000 and 49999
5 Diamond betwee 50000 and 59999
6 Executive between 80000 and 89999
7 Family between 90000 and 99999
8 Other between 60000 and 79999 or > 99999
tblMembers

AccountNum Surname Given Name
47589 Jackson Helen
12678 Hayward Alice
97166 Bond Dee
97904 Samuel Michael
27184 Anderson Louis
16041 Janes Hanora
57909 Williams Carla
etc.

I want to count all the members for each account type. Their account
number determines what type of member they are ie. a member with the
account number 35111 would be a Gold member.

The result I am looking for would like like this:

Bronze 242
Silver 32
Gold 332
Platinum 422
Diamond 11
Executive 21
Family 255
Other 71
I can't seem to think of how to structure a query that would use the
AccountNumberRa nge to group the records to count them.

Any help would be greatly appreciated.

You could make it work with some string manipulation but it would be very
very slow. Better is to add two numeric (Long) columns to tblAccountTypes
holding the starting and ending range values. Then you can join to the table
on these columns. Since the "other" account type has two ranges you will
need and extra row in the table as well.





Nov 13 '05 #2
Brenda J. <no**********@y ahoo.com> wrote in
news:2t******** *************** *********@4ax.c om:
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

AccountTypeID AccountType AccountNumberRa nge
1 Bronze between 10000 and 19999
2 Silver between 20000 and 29999
3 Gold between 30000 and 39999
4 Platinum between 40000 and 49999
5 Diamond betwee 50000 and 59999
6 Executive between 80000 and 89999
7 Family between 90000 and 99999
8 Other between 60000 and 79999 or > 99999

tblMembers

AccountNum Surname Given Name
47589 Jackson Helen
12678 Hayward Alice
97166 Bond Dee
97904 Samuel Michael
27184 Anderson Louis
16041 Janes Hanora
57909 Williams Carla
etc.

I want to count all the members for each account type. Their
account number determines what type of member they are ie. a
member with the account number 35111 would be a Gold member.

The result I am looking for would like like this:

Bronze 242
Silver 32
Gold 332
Platinum 422
Diamond 11
Executive 21
Family 255
Other 71
I can't seem to think of how to structure a query that would
use the AccountNumberRa nge to group the records to count them.

Any help would be greatly appreciated.

Brenda J.

You'll have to modify the tblaccounttypes so that your low value
and high value are in separate numeric fields.

AccountTypeID AccountType LowValue HighValue
1 Bronze 10000 19999
2 Silver 20000 29999
3 Gold 30000 39999

create a query with both tables, containing fields accountNumber,
AccountType, accounttypeID. Don't join the tables.
Criteria for accountnumber is
BETWEEN [lowvalue] AND [HighValue]

Now create a totals query grouped on accountTypeID,a ccounttype
and count of AccountNumber.

Bob Quintal
Nov 13 '05 #3
"Bob Quintal" <bq******@gener ation.net> wrote in message
news:83******** *************** *******@news.te ranews.com...

You'll have to modify the tblaccounttypes so that your low value
and high value are in separate numeric fields.

AccountTypeID AccountType LowValue HighValue
1 Bronze 10000 19999
2 Silver 20000 29999
3 Gold 30000 39999

create a query with both tables, containing fields accountNumber,
AccountType, accounttypeID. Don't join the tables.

why would you not want to join the tables ?
Nov 13 '05 #4
I would have 4 fields in tblAccountTypes , with an AccountLowerLim it
field and AccountUpperLim it field. I would then count the number of
AccountNumbers in the 2nd table between these fields.

SELECT tblAccountTypes .AccountType, Count(tblMember s.AccountNum) AS
CountOfAccountN um
FROM tblAccountTypes , tblMembers
WHERE (((tblMembers.A ccountNum) Between [AccountLowerLim it] And
[AccountUpperLim it]))
GROUP BY tblAccountTypes .AccountType;
Nov 13 '05 #5
"neptune" <bs**********@h otmail.com> wrote in message
news:c5******** *************** ***@posting.goo gle.com...
I would have 4 fields in tblAccountTypes , with an AccountLowerLim it
field and AccountUpperLim it field. I would then count the number of
AccountNumbers in the 2nd table between these fields.

SELECT tblAccountTypes .AccountType, Count(tblMember s.AccountNum) AS
CountOfAccountN um
FROM tblAccountTypes , tblMembers
WHERE (((tblMembers.A ccountNum) Between [AccountLowerLim it] And
[AccountUpperLim it]))
GROUP BY tblAccountTypes .AccountType;

I think you might be making a lot of extra work for the query engine by
aggregating a cartesian product. It would be better to do something like
this:

select a.AccountType, Count(*) as memberCount
from tblAccountTypes as a
inner join tblMembers as m on a.
on a.AccountLowerL imit < m.AccountNum
and a.AccountUpperL imit >= m.AccountNum
group by a.AccountType
Nov 13 '05 #6
"John Winterbottom" <as******@hotma il.com> wrote in
news:2k******** *****@uni-berlin.de:
"neptune" <bs**********@h otmail.com> wrote in message
news:c5******** *************** ***@posting.goo gle.com...
I would have 4 fields in tblAccountTypes , with an
AccountLowerLim it field and AccountUpperLim it field. I would
then count the number of AccountNumbers in the 2nd table
between these fields.

SELECT tblAccountTypes .AccountType,
Count(tblMember s.AccountNum) AS CountOfAccountN um
FROM tblAccountTypes , tblMembers
WHERE (((tblMembers.A ccountNum) Between [AccountLowerLim it]
And [AccountUpperLim it]))
GROUP BY tblAccountTypes .AccountType;

I think you might be making a lot of extra work for the query
engine by aggregating a cartesian product. It would be better
to do something like this:

select a.AccountType, Count(*) as memberCount
from tblAccountTypes as a
inner join tblMembers as m on a.
on a.AccountLowerL imit < m.AccountNum
and a.AccountUpperL imit >= m.AccountNum
group by a.AccountType

You may be right, but you can't do that in the query builder and
the OP's table design makes me think they aren't ready to write
SQL yet, and I believe that the query does some optimization. I
haven't found any slowdown when I use Neptune's syntax.

Bob Quintal
Nov 13 '05 #7
Thank you so much, Bob - that worked like a charm.

Stupid me - I actually recall doing something very similar in an old
app, but this morning, I just couldn't recall what it was.

Brenda J.
On Thu, 24 Jun 2004 12:53:19 GMT, Bob Quintal
<bq******@gener ation.net> wrote:
Brenda J. <no**********@y ahoo.com> wrote in
news:2t******* *************** **********@4ax. com:
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

AccountTypeID AccountType AccountNumberRa nge
1 Bronze between 10000 and 19999
2 Silver between 20000 and 29999
3 Gold between 30000 and 39999
4 Platinum between 40000 and 49999
5 Diamond betwee 50000 and 59999
6 Executive between 80000 and 89999
7 Family between 90000 and 99999
8 Other between 60000 and 79999 or > 99999

tblMembers

AccountNum Surname Given Name
47589 Jackson Helen
12678 Hayward Alice
97166 Bond Dee
97904 Samuel Michael
27184 Anderson Louis
16041 Janes Hanora
57909 Williams Carla
etc.

I want to count all the members for each account type. Their
account number determines what type of member they are ie. a
member with the account number 35111 would be a Gold member.

The result I am looking for would like like this:

Bronze 242
Silver 32
Gold 332
Platinum 422
Diamond 11
Executive 21
Family 255
Other 71
I can't seem to think of how to structure a query that would
use the AccountNumberRa nge to group the records to count them.

Any help would be greatly appreciated.

Brenda J.

You'll have to modify the tblaccounttypes so that your low value
and high value are in separate numeric fields.

AccountTypeI D AccountType LowValue HighValue
1 Bronze 10000 19999
2 Silver 20000 29999
3 Gold 30000 39999

create a query with both tables, containing fields accountNumber,
AccountType, accounttypeID. Don't join the tables.
Criteria for accountnumber is
BETWEEN [lowvalue] AND [HighValue]

Now create a totals query grouped on accountTypeID,a ccounttype
and count of AccountNumber.

Bob Quintal


Nov 13 '05 #8
You're right, John - the string solution would probably have been
quite slow, although that was my first thought, and I had been
thinking along that line.

The two fields for low and high range did the trick. Thank!

Brenda J.
On Thu, 24 Jun 2004 08:57:58 -0400, "John Winterbottom"
<as******@hotma il.com> wrote:
"Brenda J." <no**********@y ahoo.com> wrote in message
news:2t******* *************** **********@4ax. com...
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

AccountTypeID AccountType AccountNumberRa nge
1 Bronze between 10000 and 19999
2 Silver between 20000 and 29999
3 Gold between 30000 and 39999
4 Platinum between 40000 and 49999
5 Diamond betwee 50000 and 59999
6 Executive between 80000 and 89999
7 Family between 90000 and 99999
8 Other between 60000 and 79999 or > 99999

tblMembers

AccountNum Surname Given Name
47589 Jackson Helen
12678 Hayward Alice
97166 Bond Dee
97904 Samuel Michael
27184 Anderson Louis
16041 Janes Hanora
57909 Williams Carla
etc.

I want to count all the members for each account type. Their account
number determines what type of member they are ie. a member with the
account number 35111 would be a Gold member.

The result I am looking for would like like this:

Bronze 242
Silver 32
Gold 332
Platinum 422
Diamond 11
Executive 21
Family 255
Other 71
I can't seem to think of how to structure a query that would use the
AccountNumberRa nge to group the records to count them.

Any help would be greatly appreciated.

You could make it work with some string manipulation but it would be very
very slow. Better is to add two numeric (Long) columns to tblAccountTypes
holding the starting and ending range values. Then you can join to the table
on these columns. Since the "other" account type has two ranges you will
need and extra row in the table as well.






Nov 13 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2232
by: Adam i Agnieszka Gasiorowski FNORD | last post by:
I need help width formulating the most effective (in terms of processing time) SQL query to count all the "new" documents in the repository, where "new" is defined as "from 00:00:01 up to 23:59:59 today". My current query does not give me satisfactory results, it creates a visible delay in rendering of the main page of one of the departments (Drugs) :8https://hyperreal.info > site, see for yourself, notice the delay
4
14231
by: Corey | last post by:
All, I am relatively new to XML and I have what may sound like a dumb question. I want to pass a query string variable to my xml document and filter the output based on that variable. For example, if I type in www.mysite.com?geid=0000123468 I want to display only AccessReview/Report where GEID='0000123468'. I appreciate any help you can provide. Corey
4
4679
by: Chad Reid | last post by:
Hello, I have a bit of code that obviously doesn't work, but I need help creating a query that would have the same effect as if this query was working. SELECT * FROM (SELECT Count(*) AS numMarried FROM tblClients WHERE mID = 1), (SELECT Count(*) AS numSingle FROM tblClients WHERE mID = 2), (SELECT Count(*) AS numDivorced FROM tblClients WHERE mID = 3),
1
1944
by: geronimo_me | last post by:
Hi, I have a query that has the following fields: Business: ID Number: LastName: EmpNo: Hours1: Hours2:
2
4340
by: mattytee123 | last post by:
I have about 20 tables, of which I would like to do a union query and count of how many of each different code there is? The simplified verson of the table is structured like this. Code Count 1234 1 2468 1 1234 1 2468 1
14
8075
by: Darin | last post by:
I have a table that I want to delete specific records from based on data in other tables. I'm more familiar with Access '97, but am now using 2003, but the database is in 2000 format. In '97, I think I could have easily done this using joins, but I kept getting "could not delete from specified tables" errors. Some google searching has indicated I need to use a subquery. After many failed attempts with different approaches, I finally...
5
10905
by: valglad | last post by:
Hi, The question below was posted about 4 years ago and noone was able to answer it back then. I have virtually the same type of problem so would appreciate if anyone can help. Thanks ======================================================================
13
2545
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns data from a query into an array. The intent is that the following code:
3
2806
by: medic29 | last post by:
Hello, I have a query listed below which works but... The data below is what the query displays. There are hundreds of records I am bringing up and each one has between 3-6 entries displayed in this general format. The LastPFProtocolStageIndex (level reached) column will always reflect the same number for the patient. Based on the number in the first column I would like the query to display the first duplicate occurance and nothing more....
4
1312
by: trixxnixon | last post by:
Im confusing myself and need a little bit of direction, I am trying to design a query with 3 fields, “Type” “Assigned Document Team”, and “Date Submitted” There are only 6 different “type”’s and 5 different “Assigned document team”’s I want the report to show me how many of each “type” was submitted for each “assigned document team” but I don’t want the detail for each record in the report. This is an example of what i want the...
0
8192
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8696
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8358
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7188
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6119
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5571
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1504
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.