I have data in which age of all men is entered. Now I need a query to find the men who are in the ages between 15-20, 21-25, 26-30, above 30, above 40. How to make qry for this. Pl help. The age between may be fixed as shown or can make two text boxes to type ages and click ok to find.
There is a little known and rarely used Function (Partition()) that can greatly simplify matters for you. Suppose you have a Table named tblAges with a Field named [Age]. You wish to obtain the Aggregate Totals for all Age Groups within the Ages of 15 to 100 at 5 Year Intervals. The SQL and Test results are as follows: - SELECT DISTINCTROW Partition([Age],15,100,6) AS Age_Range, Count(tblAges.Age) AS Range_Count
-
FROM tblAges
-
GROUP BY Partition([Age],15,100,6);
-
Age_Range Range_Count
-
11: 15 2
-
16: 20 3
-
21: 25 7
-
31: 35 8
-
51: 55 4
-
56: 60 5
-
76: 80 2
-
81: 85 1
PS (NeoPa) The Partition() Function from another post is helpful to include in Best Answer.
9 13001
You can use the WHERE clause to filter on whatever range you want to use.
For example: - SELECT *
-
FROM someTable
-
WHERE Age BETWEEN 15 AND 20
NeoPa 32,556
Expert Mod 16PB
Before you go any further though, you should consider the idea of storing ages at all. Dates of Birth will always reflect the age, whereas Ages will only ever be correct until their next birthdays.
To test for ages in such a scenario : - SELECT *
-
FROM [YourTable]
-
WHERE ([DateOfBirth] Between DateAdd('yyyy',-20, Date()) And DateAdd('yyyy',-15, Date()))
There is a little known and rarely used Function (Partition()) that can greatly simplify matters for you. Suppose you have a Table named tblAges with a Field named [Age]. You wish to obtain the Aggregate Totals for all Age Groups within the Ages of 15 to 100 at 5 Year Intervals. The SQL and Test results are as follows: - SELECT DISTINCTROW Partition([Age],15,100,6) AS Age_Range, Count(tblAges.Age) AS Range_Count
-
FROM tblAges
-
GROUP BY Partition([Age],15,100,6);
-
Age_Range Range_Count
-
11: 15 2
-
16: 20 3
-
21: 25 7
-
31: 35 8
-
51: 55 4
-
56: 60 5
-
76: 80 2
-
81: 85 1
PS (NeoPa) The Partition() Function from another post is helpful to include in Best Answer.
NeoPa 32,556
Expert Mod 16PB
Excellent idea ADezii. I'd still recommend the use of [DoB] data rather than [Age] though. Partition will handle either way perfectly well.
NeoPa 32,556
Expert Mod 16PB
Not at all. Very relevant I'd say. Good for you.
I prefer to use more accurate Age Calculation Logic as opposed to Birth Days, as in: -
Function fCalculateAge(varDOB As Variant, Optional varAsOfThisDate As Variant) As Variant
-
'Purpose: Return the Age in years.
-
'Arguments: varDOB = Date Of Birth
-
'varAsOfThisDate = the date to calculate the age at, or today if missing.
-
-
'The DateDiff() Function simply subtracts the Year parts of the Dates, without
-
'reference to the Month or Day. This means we need to subtract one if the person
-
'has not has their birthday this year. This can be handled by a Boolean Expression
-
'that returns either -1 or 0 depending on whether or not an individual's
-
'birthday occurred within the Year.
-
-
'Return: Whole number of years.
-
Dim dteDOB As Date
-
Dim dteAsOf As Date
-
Dim dteBDay As Date 'Birthday in the year of calculation.
-
-
fCalculateAge = Null 'Initialize to Null
-
-
'Validate Parameters
-
If IsDate(varDOB) Then
-
dteDOB = varDOB
-
-
If Not IsDate(varAsOfThisDate) Then 'Date to calculate age from.
-
dteAsOf = Date 'Use the Current Date
-
Else
-
dteAsOf = varAsOfThisDate 'Valid As Of Date
-
End If
-
-
If dteAsOf >= dteDOB Then 'Calculate only if it's after person was born.
-
dteBDay = DateSerial(Year(dteAsOf), Month(dteDOB), Day(dteDOB))
-
fCalculateAge = DateDiff("yyyy", dteDOB, dteAsOf) + (dteBDay > dteAsOf)
-
End If
-
End If
-
End Function
NeoPa 32,556
Expert Mod 16PB ADezii:
I prefer to use more accurate Age Calculation Logic as opposed to Birth Days, as in:
Using DateDiff() can certainly lead you into muddy waters ADezii, yet that isn't the case with DateAdd() (as used in post #3). Although the actual values used may want to be tweaked if that were the approach used, rather than using Partition() which would be my favourite, as the example shown cuts off after the 20th birthday, the accuracy is fine.
To produce an accurate [Age] value though, you would not want to rely solely on DateDiff(), clearly.
Rather than a bespoke function though, I'd suggest slightly more complicated SQL as in : - SELECT DateDiff('yyyy',[DateOfBirth],Date())-IIf(DateAdd('yyyy',DateDiff('yyyy',[DateOfBirth],Date()),[DateOfBirth])>Date(),1,0) AS [Age]
This will return a fully accurate [Age] based on the [DateOfBirth] value.
Bravo! A much cleaner and more efficient approach. Nice job, NeoPa.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Paolo Invernizzi |
last post by:
Spambayes CVS, Python 2.2.3, FreeBSD 5.1
On my machine I'm lunching the pop3proxy with cron at startup, and all
is fine.
But... sometimes during startup, or after a one or two days, the...
|
by: aroy |
last post by:
Hi,
Need help in optimizing a query in SQL Server.
Following is the problem statement.
There are two tables;
1st table (t1) has a KEY ( char(8) ) column, with a clustered index.
this is not...
|
by: Rathtap |
last post by:
This query returns only those starting with A and B. Why?
SELECT name FROM mytable WHERE name BETWEEN 'A' AND 'C'
|
by: jqq |
last post by:
SQL2K on W2Kserver
I need some help revamping a rather complicated query. I've given the
table and existing query information below. (FYI, changing the
database structure is right out.)
The...
|
by: Kristen |
last post by:
I need to query an Access database via VB and get a count of all the
calls where the start time is between a 2 certain days/times.
For example, all the calls that occurred from Saturday @ 8am to...
|
by: anilcool |
last post by:
Hi all,
This is probably a simple problem for most of you.. Let me know if you
have any pointers for me. I am new to DB2.
In my stored procedure I want to select records that match a range of...
|
by: mikevde |
last post by:
Hi,
I want to select data from a range of dates i.e over a 1 month period
I've tried this:
SELECT * FROM table WHERE DATE_SUB(Date(), INTERVAL 30 DAY)
but the query isnt working as it is...
|
by: flumpuk |
last post by:
Hi
My job currently requires me to enter data from 300+ forms a month.
The system which we used in Excel was slow , and theprevious guy had
three workbooks for this job .
I have created...
|
by: matias.cornejo |
last post by:
I have a table with 1.000.000 registers and I want to find the
register in a position 25.000. I don't want to use a cursor, how can I
do it in other way?
Thenks in advance
|
by: Octo Siburian |
last post by:
How do I interpret the declaration time 'AM' and 'PM' in Ms.access Query
I have Query Select in vb6.0
to get data from Ms.access database
SQLSelectdbRAS = "SELECT a.DN, b.DIN, c.PIN,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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...
|
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,...
| |