Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

making age group query work

623 views
Skip to first unread message

Linda in Iowa

unread,
Jul 14, 2009, 10:53:52 PM7/14/09
to
I recently posted this and received a reply as follows. Can anyone tell me
how to make it work as I have not been able to get correct results if any.
I am using Access 2003.

Here is my original post:
----------------------------
I have an age field that is calculated from a birthdate.
I want to count the number of records in age groups such as 10-19, 20-29,
30-39, 40-49.

the Birthdate field has date entered as mm/dd/yyyy
The Age field is calculated by using ((Now()-[MbrBirthdate])/365)

When I specify an age group in the criteria area of the calculated age field
and Count on the Birthdate field it will show each age and count it as 1. I
want the total number of records in that group.

Here is the SQL of the query I currently have:

SELECT DISTINCTROW ((Now()-[MbrBirthdate])/365) AS [Main member age],
Count(MBRS.MbrBirthdate) AS CountOfMbrBirthdate
FROM MBRS
GROUP BY ((Now()-[MbrBirthdate])/365), MBRS.Memtype, MBRS.ExpDate
HAVING (((((Now()-[MbrBirthdate])/365))>69.9 And
(((Now()-[MbrBirthdate])/365))<80) AND ((MBRS.Memtype)<>"comp") AND
((MBRS.ExpDate)>Date()))
ORDER BY ((Now()-[MbrBirthdate])/365) DESC;


Also is it possible to show the counts for all age groups in one query?

Here is the response I am trying to get to work.
---------------------------------------------------
For the most flexibility, I'd suggest an AgeGroups table with two fields:
Age
(Long Integer) and Agegroup (text), with values like

0 "Child"
10 "10-19"
20 "20-29"

and so on.

Since a year isn't exactly 365 days (leap years!), dividing by 365 won't
give
an exact age as of the previous birthday. Instead use an expression:

DateDiff("yyyy", [MbrBirthdate], Date()) - IIF(Format([MbrBirthdate],
"mmdd")
> Format(Date(), "mmdd"), 1, 0)

Include the AgeGroups table in the query with this expression as a criterion
on Age:

>= DateDiff("yyyy", [MbrBirthdate], Date()) - IIF(Format([MbrBirthdate],
>"mmdd") > Format(Date(), "mmdd"), 1, 0)

Sort ascending by Age and set the Top Values property of the query to 1 to
select the age group.
--


Duane Hookom

unread,
Jul 15, 2009, 10:19:03 AM7/15/09
to
How far did you get? Did you create a table of age ranges? I would have added
a field to specify the min and max ages:

tblAgeGroups
AgeMin AgeMax AgeGroup
0 9 "Child"
10 19 "10-19"
.... etc ...
You would calculate the age as a column in your query and set the criteria
under this age column to:
Between AgeMin and AgeMax

--
Duane Hookom
Microsoft Access MVP

John Spencer

unread,
Jul 15, 2009, 11:18:36 AM7/15/09
to
If you are trying to count by decades 0 to 9, 10 to 19, etc you can accomplish
this with just some math

SELECT DateDiff("yyyy", [MbrBirthdate], Date()) - IIF(Format([MbrBirthdate],
>"mmdd") > Format(Date(), "mmdd"), 1, 0)\10 AS [Decade],
Count(MBRS.MbrBirthdate) AS CountByDecade
FROM MBRS
WHERE MBRS.Memtype<>"comp" AND MBRS.ExpDate>Date()
GROUP BY DateDiff("yyyy", [MbrBirthdate], Date()) - IIF(Format([MbrBirthdate],
>"mmdd") > Format(Date(), "mmdd"), 1, 0)\10

If you want to use your formula for age (which is a bit less accurate then)

SELECT ((DATE()-[MbrBirthdate])/365.25)\10 AS [Decade],
Count(MBRS.MbrBirthdate) AS CountDecade
FROM MBRS
WHERE MBRS.Memtype<>"comp" AND MBRS.ExpDate>Date()
GROUP BY ((DATE()-[MbrBirthdate])/365.25)\10
ORDER BY ((DATE()-[MbrBirthdate])/365.25)\10 DESC;

If you need date ranges that are different than that you would need a table
similar to Duane's. I would modify it slightly and make ageMax the cutoff
value that is for the first group I would have 10 not 9. I would then test in
the query to make sure the Age Group was LESS than the AgeMax. Modifying your
query to use that table

SELECT tblAgeGroup.AgeGroup, tblAgeGroup.AgeMin
Count(MBRS.MbrBirthdate) AS CountOfMbrBirthdate
FROM MBRS INNER JOIN AgeGroup
ON ((Now()-MBRS.[MbrBirthdate])/365) >= tblAgeGroup.AgeMin
AND ((Now()-MBRS.[MbrBirthdate])/365) < tblAgeGroup.AgeMax
WHERE MBRS.Memtype<>"comp" AND MBRS.ExpDate>Date()
GROUP BY tblAgeGroup.AgeGroup, tblAgeGroup.AgeMin
ORDER BY tblAgeGroup.AgeMin DESC;


John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County

Linda in Iowa

unread,
Jul 15, 2009, 6:51:13 PM7/15/09
to
Your DateDiff code give me a Syntax error (missing operator) in query
expression 'DateDiff("yyyy", [MbrBirthdate], Date()) -
IIF(Format([MbrBirthdate],
>"mmdd") > Format(Date(), "mmdd"), 1, 0)\10'.

The SELECT ((DATE()-[MbrBirthdate])/365.25)\10 AS [Decade], code works fine
but I would like to see how the DateDiff works.

Thanks.


"John Spencer" <spe...@chpdm.edu> wrote in message
news:%232E3G%23VBKH...@TK2MSFTNGP03.phx.gbl...

John Spencer

unread,
Jul 15, 2009, 7:26:40 PM7/15/09
to
Looks like an extra > crept into the expression.

DateDiff("yyyy",[MbrBirthdate], Date())-
IIF(Format([MbrBirthdate],"mmdd") > Format(Date(),"mmdd"),1,0)

So unless I've made some other egregious blunder the query should read:

SELECT DateDiff("yyyy",[MbrBirthdate],Date()) -
IIF(Format([MbrBirthdate],"mmdd")>Format(Date(),"mmdd"),1,0)\10 AS [Decade],


Count(MBRS.MbrBirthdate) AS CountByDecade
FROM MBRS
WHERE MBRS.Memtype<>"comp" AND MBRS.ExpDate>Date()
GROUP BY DateDiff("yyyy", [MbrBirthdate], Date()) -
IIF(Format([MbrBirthdate],"mmdd")>Format(Date(),"mmdd"),1,0)\10

'====================================================


John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County

'====================================================

Linda in Iowa

unread,
Jul 15, 2009, 11:33:19 PM7/15/09
to
Thanks again, but the result I get is the number of members for each year of
age. I was hoping for the number of members for each 10 year group: 0-9,
10-19, 20-29 etc.
Linda


"John Spencer" <spe...@chpdm.umbc> wrote in message
news:e4bbtNaB...@TK2MSFTNGP05.phx.gbl...

John Spencer

unread,
Jul 16, 2009, 11:01:48 AM7/16/09
to
Did you do the integer division by 10?

This gives you the age in years.


DateDiff("yyyy",[MbrBirthdate], Date())-
IIF(Format([MbrBirthdate],"mmdd") > Format(Date(),"mmdd"),1,0)

This gives you the age in decades completed - 0 being 0 to 9, 1 being 10 to
19, etc.


DateDiff("yyyy",[MbrBirthdate], Date())-

IIF(Format([MbrBirthdate],"mmdd") > Format(Date(),"mmdd"),1,0) \ 10

You are doing integer division when you divide by 10 using the reverse slash.

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County

Linda in Iowa

unread,
Jul 16, 2009, 12:49:05 PM7/16/09
to
I solved it by putting parantheses in front of DateDiff and another one just
before \10.
So it looks like this now.
(DateDiff("yyyy",[MbrBirthdate], Date())-
IIF(Format([MbrBirthdate],"mmdd") > Format(Date(),"mmdd"),1,0)) \ 10

Thanks again.

"John Spencer" <spe...@chpdm.edu> wrote in message

news:OS$8YZiBK...@TK2MSFTNGP04.phx.gbl...

0 new messages