473,379 Members | 1,367 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,379 software developers and data experts.

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 AccountNumberRange
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
AccountNumberRange to group the records to count them.

Any help would be greatly appreciated.

Brenda J.

Nov 13 '05 #1
8 1717
"Brenda J." <no**********@yahoo.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 AccountNumberRange
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
AccountNumberRange 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**********@yahoo.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 AccountNumberRange
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 AccountNumberRange 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,accounttype
and count of AccountNumber.

Bob Quintal
Nov 13 '05 #3
"Bob Quintal" <bq******@generation.net> wrote in message
news:83******************************@news.teranew s.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 AccountLowerLimit
field and AccountUpperLimit field. I would then count the number of
AccountNumbers in the 2nd table between these fields.

SELECT tblAccountTypes.AccountType, Count(tblMembers.AccountNum) AS
CountOfAccountNum
FROM tblAccountTypes, tblMembers
WHERE (((tblMembers.AccountNum) Between [AccountLowerLimit] And
[AccountUpperLimit]))
GROUP BY tblAccountTypes.AccountType;
Nov 13 '05 #5
"neptune" <bs**********@hotmail.com> wrote in message
news:c5**************************@posting.google.c om...
I would have 4 fields in tblAccountTypes, with an AccountLowerLimit
field and AccountUpperLimit field. I would then count the number of
AccountNumbers in the 2nd table between these fields.

SELECT tblAccountTypes.AccountType, Count(tblMembers.AccountNum) AS
CountOfAccountNum
FROM tblAccountTypes, tblMembers
WHERE (((tblMembers.AccountNum) Between [AccountLowerLimit] And
[AccountUpperLimit]))
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.AccountLowerLimit < m.AccountNum
and a.AccountUpperLimit >= m.AccountNum
group by a.AccountType
Nov 13 '05 #6
"John Winterbottom" <as******@hotmail.com> wrote in
news:2k*************@uni-berlin.de:
"neptune" <bs**********@hotmail.com> wrote in message
news:c5**************************@posting.google.c om...
I would have 4 fields in tblAccountTypes, with an
AccountLowerLimit field and AccountUpperLimit field. I would
then count the number of AccountNumbers in the 2nd table
between these fields.

SELECT tblAccountTypes.AccountType,
Count(tblMembers.AccountNum) AS CountOfAccountNum
FROM tblAccountTypes, tblMembers
WHERE (((tblMembers.AccountNum) Between [AccountLowerLimit]
And [AccountUpperLimit]))
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.AccountLowerLimit < m.AccountNum
and a.AccountUpperLimit >= 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******@generation.net> wrote:
Brenda J. <no**********@yahoo.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 AccountNumberRange
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 AccountNumberRange 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,accounttype
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******@hotmail.com> wrote:
"Brenda J." <no**********@yahoo.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 AccountNumberRange
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
AccountNumberRange 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
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...
4
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...
4
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...
1
by: geronimo_me | last post by:
Hi, I have a query that has the following fields: Business: ID Number: LastName: EmpNo: Hours1: Hours2:
2
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 ...
14
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...
5
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
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...
3
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...
4
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 ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.