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

grouping data by age

Hi
I am having trouble getting my head around how to go about group ages
I have a querytable which holds the information that I need that is the age field What I want to do is group this by ages groups eg under 19, 20 to 29, 30 to 39 etc.
I then need to put this information into a crosstab query to do the count
I have tried to do this several ways but am at a loss as to why I am not winning
I would appreciate any help that you can give.
If helping please state exactly where I enter the information as I think this is where I am going wrong.
Jan 18 '10 #1

✓ answered by nico5038

I would use an Age function (stored in a module) like:
Expand|Select|Wrap|Line Numbers
  1. Function Age(Bdate, DateToday) As Integer
  2. ' Returns the Age in years between 2 dates
  3. ' Doesn't handle negative date ranges i.e. Bdate > DateToday
  4.  
  5.     If Month(DateToday) < Month(Bdate) Or (Month(DateToday) = _
  6.                 Month(Bdate) And Day(DateToday) < Day(Bdate)) Then
  7.             Age = Year(DateToday) - Year(Bdate) - 1
  8.     Else
  9.             Age = Year(DateToday) - Year(Bdate)
  10.     End If
  11. End Function
From http://www.mvps.org/access/datetime/date0001.htm (good site with many good samples and info!)

Now your query can use:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblAgeGroup.AgeGroup, Count(*) AS MembersInGroup
  2. FROM MBRS, AgeGroup
  3. WHERE Age(MBRS.[MbrBirthdate] between tblAgeGroup.AgeMin AND tblAgeGroup.AgeMax
  4. AND MBRS.Memtype<>"comp" 
  5. AND MBRS.ExpDate>Date()
  6. GROUP BY tblAgeGroup.AgeGroup
  7. ORDER BY 1 DESC;
When the MBRS.ExpDate can be empty, an additional IsNull test should be added...

Getting the idea ?

Nic;o)

23 3585
nico5038
3,080 Expert 2GB
Multiple ways of achieving this, but the most flexible is to create a table with [AgeFrom] and [AgeTo] and [AgeGroup].
Use these fields in the query to join with the original table with [Age] like:
Expand|Select|Wrap|Line Numbers
  1. SELECT .....
  2. FROM tblAge, tblAgeGroup
  3. WHERE [Age] between [AgeFrom] and [AgeTo] 
  4.  
It's up to you to make the table covering all ages from your tblAge, but I guess that's obvious.

Nic;o)
Jan 18 '10 #2
is there away to put an expression or function in that will group even in excel you can group with out making new tables I would perfer to have a function or something like
Jan 18 '10 #3
nico5038
3,080 Expert 2GB
Press the "E" (Sigma) looking button to create a Group By query for totalling field(s). Access will add in query design mode a row for the aggregation functions like Sum, Count, Max, etc.
Also check the help file for the Group By queries when you haven´t used them before.

Nic;o)
Jan 18 '10 #4
thanking you I will try that
Jan 18 '10 #5
NeoPa
32,556 Expert Mod 16PB
Can I suggest that, though the table approach may seem a little incongruous, or maybe just overkill, that it is actually a perfect database solution. I recommend you reconsider. Such things separate the men from the boys in database work.
Jan 18 '10 #6
ADezii
8,834 Expert 8TB
There is a little known, but very useful Function, that deals specifically with this issue. It is called the Partition() Function and it can be incorporated directly into an SQL Statement with no need for any external Tables. All you need is to pass to the Function a Numeric Field, a Start, Stop, and Interval Value. Partition(0 will total the number of results for specified Ranges. I wrote a Tip on this very Topic a little time ago, and I will not go into detail relating to it since the Link to the Tip says it all.
http://bytes.com/topic/access/insigh...ition-function
Jan 19 '10 #7
I do so thank both ADezil & NeoPa for your advise I will do some more reading I really need this to work To date everyone has had their own DB and they have been hand writing their reports About 6 months ago I did an excel program for the reports which works well but they still have to double handle the information. I have used Access in the past but not for this type of reporting and I must say I am at a loss at the moment. So I do appreciate all the support and suggestions you are making I do so hope I am not to dumb and old to figure it out thanks again Just Me
Jan 19 '10 #8
nico5038
3,080 Expert 2GB
First create a query to get the agegroup available in a column.
Next create a crosstable query using the previously created query.

Let me know when you get stuck and where you get stuck.

Nic;o)
Jan 19 '10 #9
I can get the first Query but the cross table query say the are to many rows and if I use columns to many columns. I was trying to pull data from different tables but that did not work so I did a querytable I think they are referred to as action queries i this It kept saying that something was opened in exclusive use.
I have most of the other things I need for my report they are Nationality, Benifits Employment Postcode and gender. but I just cant seem to get the age one. The others I had to do in seperate crosstab queries and I am hoping that the report will pull it all together. Maybe I should take a weeks break and come back to it. I am just not winning at all. And then to day the print report form I had set up that I use all the time stopped working and I cant see where. its just a simple date from date to with buttons from week month year but for some reason it just does not want to work now. one other thing can I lock the data on a form but have a button that allows editing and new entries. I have never worried about this function before I thought it was a good idea but it lock the new record also I just a little worried about some of the staff that I only want them to look up. They are mostly little old ladies that think they know everything and they dont listen to what you are saying there for stuff most stuff up thanks for your time I really did not expect this to be such a big deal
Jan 19 '10 #10
nico5038
3,080 Expert 2GB
Hmm, can you post the SQL text of both queries so we can have a look ?

Nic;o)
Jan 19 '10 #11
Last night I deleted everything to do with the age queries and I was going to start fresh again today. If this does not work I will gladly send it to you. I am so grateful you are interested. I dont even have a sole here that has any idea of what I am talking about.
Jan 19 '10 #12
nico5038
3,080 Expert 2GB
OK, lets start with the qryAgeGroup. Try one of the methods and post the result here so we can have a look.

Nic;o)
Jan 19 '10 #13
Ok i can make a query with the details from Client Detail and Assistance Tables The details are From Client Details [Clientno] and [age] From Assistance [Date] This works fine it gives me all the info I need. But when I try to do the other query adding the Age Group stuff I get than the file in opened in exclusive use I can not see why
Jan 20 '10 #14
Question I have check to make sure the data base is not share I have also checked that the startup is allowing changes Is there something that I dont know about in regards to exclusive use. I am now unable to make a new form its say that I dont have exclusive us and my changes may not be saved
I think I am losing the plot here. I have been checking the integerity of the data base all the way have I missed something or have I just done the wrong thing somewhere sorry to be a pain
Jan 20 '10 #15
Could it have something to do with my relationships
Jan 20 '10 #16
nico5038
3,080 Expert 2GB
A SELECT query can't block the database.
Can you post the SQL text here ?

Nic;o)
Jan 20 '10 #17
NeoPa
32,556 Expert Mod 16PB
May I point out that unless you take some more care over your posts there is a danger that many of our experts will ignore your posts in future due to the obvious lack of care taken before posting, even to formulate your thoughts into readable English. I can tell you from my own perspective that I don't appreciate having to read most of your posts 3 times just to make some kind of sense. I suggest, if you're interested in receiving help in future, you take care to avoid giving the impression you value your own time so much more highly than the time you waste of our experts.

-Administrator.
Jan 21 '10 #18
I am trying not to waste your time. I have had some pressing work that has to be completed and that is why I have not posted anything. I have also been doing some reading trying to work out which is the best way to work out my problem as my data base will not let me do anything with it at the moment.
I have tried to export my tables to another data file but i am still having the same problem. Tonight I have spent on this. I am extremely sorry if I have upset you I am trying very hard to do my best. I really do appreciate all the time you have given me and my time is not more valuable than yours I regret giving that impression. Can you please give me some time to figure out what I have done. and accept my apology
Jan 21 '10 #19
In my reading today I found this I have not been able to try it out I was wondering what you thought of it
If you are trying to count by decades 0 to 9, 10 to 19, etc you can accomplish
this with just some math
Expand|Select|Wrap|Line Numbers
  1. SELECT DateDiff("yyyy", [MbrBirthdate], Date()) - IIF(Format([MbrBirthdate], 
  2.  >"mmdd") > Format(Date(), "mmdd"), 1, 0)\10 AS [Decade], 
  3. Count(MBRS.MbrBirthdate) AS CountByDecade 
  4. FROM MBRS 
  5. WHERE MBRS.Memtype<>"comp" AND MBRS.ExpDate>Date() 
  6. GROUP BY DateDiff("yyyy", [MbrBirthdate], Date()) - IIF(Format([MbrBirthdate], 
  7.  >"mmdd") > Format(Date(), "mmdd"), 1, 0)\10
If you want to use your formula for age (which is a bit less accurate then)
Expand|Select|Wrap|Line Numbers
  1. SELECT ((DATE()-[MbrBirthdate])/365.25)\10 AS [Decade], 
  2. Count(MBRS.MbrBirthdate) AS CountDecade 
  3. FROM MBRS 
  4. WHERE MBRS.Memtype<>"comp" AND MBRS.ExpDate>Date() 
  5. GROUP BY ((DATE()-[MbrBirthdate])/365.25)\10 
  6. 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
Expand|Select|Wrap|Line Numbers
  1. SELECT tblAgeGroup.AgeGroup, tblAgeGroup.AgeMin 
  2. Count(MBRS.MbrBirthdate) AS CountOfMbrBirthdate 
  3. FROM MBRS INNER JOIN AgeGroup 
  4. ON ((Now()-MBRS.[MbrBirthdate])/365) >= tblAgeGroup.AgeMin 
  5. AND  ((Now()-MBRS.[MbrBirthdate])/365) < tblAgeGroup.AgeMax 
  6. WHERE MBRS.Memtype<>"comp" AND MBRS.ExpDate>Date() 
  7. GROUP BY tblAgeGroup.AgeGroup, tblAgeGroup.AgeMin 
  8. ORDER BY tblAgeGroup.AgeMin DESC;
Jan 21 '10 #20
nico5038
3,080 Expert 2GB
I would use an Age function (stored in a module) like:
Expand|Select|Wrap|Line Numbers
  1. Function Age(Bdate, DateToday) As Integer
  2. ' Returns the Age in years between 2 dates
  3. ' Doesn't handle negative date ranges i.e. Bdate > DateToday
  4.  
  5.     If Month(DateToday) < Month(Bdate) Or (Month(DateToday) = _
  6.                 Month(Bdate) And Day(DateToday) < Day(Bdate)) Then
  7.             Age = Year(DateToday) - Year(Bdate) - 1
  8.     Else
  9.             Age = Year(DateToday) - Year(Bdate)
  10.     End If
  11. End Function
From http://www.mvps.org/access/datetime/date0001.htm (good site with many good samples and info!)

Now your query can use:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblAgeGroup.AgeGroup, Count(*) AS MembersInGroup
  2. FROM MBRS, AgeGroup
  3. WHERE Age(MBRS.[MbrBirthdate] between tblAgeGroup.AgeMin AND tblAgeGroup.AgeMax
  4. AND MBRS.Memtype<>"comp" 
  5. AND MBRS.ExpDate>Date()
  6. GROUP BY tblAgeGroup.AgeGroup
  7. ORDER BY 1 DESC;
When the MBRS.ExpDate can be empty, an additional IsNull test should be added...

Getting the idea ?

Nic;o)
Jan 21 '10 #21
this has worked thanking you very much for your time and patience.
Jan 22 '10 #22
nico5038
3,080 Expert 2GB
Glad I could help. Success with your application "Down under" :-)

Nic;o)
Jan 22 '10 #23
NeoPa
32,556 Expert Mod 16PB
@JustMe43
I'm not upset. I'm merely trying to advise you how your posts can be seen from this side of the fence. Your posts have not been breaking any rules as such, so my moderator hat (& truncheon) were not needed.

If all your future posts are as free of confusion and typos as this one was, then I see no reason to worry.

I'm also pleased to see that you have received some help in my absence (which was related to me rather than trying to avoid this particular thread) over the last number of days. I will arrange to have Nico's helpful post set as the Best Answer to reflect this.
Jan 24 '10 #24

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

Similar topics

1
by: Jonathan Woods | last post by:
Hi there, How do i handle grouping same level of data at run time? or any idea? user can view data according to their selection group on the windows form. for example: In customer table,...
2
by: Stephen Witter | last post by:
I am using WriteXml to output data to an xml file, and and XML web control to display it. The code runs fine, however my groups are being ignored. For Instance, the current output is: ...
5
by: Hemant Shah | last post by:
Folks, We have table that cointains timesheet entries for the employees. Given user name, start date, and end date I want to get sum of hours for each project, for each week. Exmaple: Table...
1
by: gozzer101 | last post by:
Hello! I have a small problem with grouping data on reports. The situation is that I have an PersonIDnumber, PersonName, NoPurchases and ProductNumbersAllocated. The person ID is just any number,...
7
by: derekdeben | last post by:
Hi, I have created a report that totals the number of days it took a product to ship by percentage by a date range for a specific location. My data comes from a query with the following headers: ...
1
by: h2lm2t | last post by:
Could anyone please take a look at this? I have a table with 3 columns: ID, ZIP and Count as below: Original Table ID ZIP Count 1 00001 12 2 00002 12 3 00003 ...
0
by: icanhelp33 | last post by:
I am using a method to group data in Gridview. I am able to compare one sortexpression with the gridview field. I would also like to compare sortexpression2 with another gridviewfield. How is that...
4
by: singhals | last post by:
I have hundereds of rows of data which goes something like this Date Value 12/31/2009 52 1/1/2009 455 1/1/2005 497 1/1/2009 ...
0
by: AndrewBuchan | last post by:
I am running a Stored Procedure which populates a DataTable. The data returned is 8 columns, varying number of rows. One column in the row defines the "Agent" of that row - basically who manages it...
5
by: Andrew Shearer | last post by:
I have hit the 2GB limit in Excel and am now looking at a solution in Access 2007. The issue I'm facing is replicating what I'm doing in Excel into Access. The main challenge and my question is how...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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
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...
0
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,...
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...

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.