473,395 Members | 2,713 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,395 software developers and data experts.

To make Age Range Query to select men between these ages

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.
Nov 21 '12 #1

✓ answered by ADezii

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:
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCTROW Partition([Age],15,100,6) AS Age_Range, Count(tblAges.Age) AS Range_Count
  2. FROM tblAges
  3. GROUP BY Partition([Age],15,100,6);
Expand|Select|Wrap|Line Numbers
  1. Age_Range    Range_Count
  2. 11: 15            2
  3. 16: 20            3
  4. 21: 25            7
  5. 31: 35            8
  6. 51: 55            4
  7. 56: 60            5
  8. 76: 80            2
  9. 81: 85            1
PS (NeoPa) The Partition() Function from another post is helpful to include in Best Answer.

9 13001
Rabbit
12,516 Expert Mod 8TB
You can use the WHERE clause to filter on whatever range you want to use.

For example:
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM someTable
  3. WHERE Age BETWEEN 15 AND 20
Nov 21 '12 #2
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 :
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM   [YourTable]
  3. WHERE  ([DateOfBirth] Between DateAdd('yyyy',-20, Date()) And DateAdd('yyyy',-15, Date()))
Nov 21 '12 #3
ADezii
8,834 Expert 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCTROW Partition([Age],15,100,6) AS Age_Range, Count(tblAges.Age) AS Range_Count
  2. FROM tblAges
  3. GROUP BY Partition([Age],15,100,6);
Expand|Select|Wrap|Line Numbers
  1. Age_Range    Range_Count
  2. 11: 15            2
  3. 16: 20            3
  4. 21: 25            7
  5. 31: 35            8
  6. 51: 55            4
  7. 56: 60            5
  8. 76: 80            2
  9. 81: 85            1
PS (NeoPa) The Partition() Function from another post is helpful to include in Best Answer.
Nov 28 '12 #4
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.
Nov 28 '12 #5
ADezii
8,834 Expert 8TB
An Article exists in Bytes that will provide expanded information on the Partition() Function if anyone is interested. Sorry for going Off-Topic a little, but I feel that it is related to this Thread.
http://bytes.com/topic/access/insigh...ition-function
Nov 29 '12 #6
NeoPa
32,556 Expert Mod 16PB
Not at all. Very relevant I'd say. Good for you.
Nov 29 '12 #7
ADezii
8,834 Expert 8TB
I prefer to use more accurate Age Calculation Logic as opposed to Birth Days, as in:
Expand|Select|Wrap|Line Numbers
  1. Function fCalculateAge(varDOB As Variant, Optional varAsOfThisDate As Variant) As Variant
  2. 'Purpose:   Return the Age in years.
  3. 'Arguments: varDOB = Date Of Birth
  4. 'varAsOfThisDate = the date to calculate the age at, or today if missing.
  5.  
  6. 'The DateDiff() Function simply subtracts the Year parts of the Dates, without
  7. 'reference to the Month or Day. This means we need to subtract one if the person
  8. 'has not has their birthday this year. This can be handled by a Boolean Expression
  9. 'that returns either -1 or 0 depending on whether or not an individual's
  10. 'birthday occurred within the Year.
  11.  
  12. 'Return:    Whole number of years.
  13. Dim dteDOB As Date
  14. Dim dteAsOf As Date
  15. Dim dteBDay As Date     'Birthday in the year of calculation.
  16.  
  17. fCalculateAge = Null    'Initialize to Null
  18.  
  19. 'Validate Parameters
  20. If IsDate(varDOB) Then
  21.   dteDOB = varDOB
  22.  
  23.   If Not IsDate(varAsOfThisDate) Then  'Date to calculate age from.
  24.     dteAsOf = Date      'Use the Current Date
  25.   Else
  26.     dteAsOf = varAsOfThisDate       'Valid As Of Date
  27.   End If
  28.  
  29.   If dteAsOf >= dteDOB Then      'Calculate only if it's after person was born.
  30.     dteBDay = DateSerial(Year(dteAsOf), Month(dteDOB), Day(dteDOB))
  31.       fCalculateAge = DateDiff("yyyy", dteDOB, dteAsOf) + (dteBDay > dteAsOf)
  32.   End If
  33. End If
  34. End Function
Nov 29 '12 #8
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 :
Expand|Select|Wrap|Line Numbers
  1. 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.
Nov 29 '12 #9
ADezii
8,834 Expert 8TB
Bravo! A much cleaner and more efficient approach. Nice job, NeoPa.
Nov 29 '12 #10

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

Similar topics

0
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...
3
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...
3
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'
10
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...
3
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...
3
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...
1
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...
1
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...
4
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
2
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,...
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:
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
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,...
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.