Solved

MYSQL Select Statement to Return Groups of Ages?

Posted on 2004-08-16
2
738 Views
Last Modified: 2006-11-17
Can you help?  I have this working mySQL Select statement that returns a ONE ROW result ... that counts the number of users between the age of 18 and 25.

SELECT Count(*) AS CountOf18to25s
FROM opinionvotes o, users u,  demographics  d
WHERE o.userID = u.userID
AND u.userID = d.userID
AND o.opinion_id = 232
AND (d.birthyear + 1900) BETWEEN (Year(Now()) - 25) AND (Year(Now()) - 18)


I ALSO need to find out how many users are between these age bands ALSO:
26 to 35
36 to 45
46 to 55
56 to 65
>65


RATHER than performing 6 seperate SELECT statements ... ONE for EACH of these AGE bands (including the 18 to 25) ... I'm HOPING a Select Statement can be written that will return 6 ROWS ... with ONE ROW for EACH of the above age bands.  IS this possible?

Thanks!
Each 'record' in the demographics table contains a field called 'birthyear' which is the TWO DIGIT YEAR of BIRTH! SO I need to ADD 1900 to the YEAR i.e. 65 + 1900 = 1965 and SUBTRACT the 1965 from the current year (2004) to get 39 years of age.

Richard
0
Comment
Question by:rcbuchanan
2 Comments
 
LVL 6

Accepted Solution

by:
andreif earned 500 total points
ID: 11815665
Hi, here is my solution

1. create extra table for age groups:

create table agegroups (startage int, endage int, title varchar(25));
and add all intervals there, like this:
18, 25, '18-25'
26, 35, '26-35'
...
66, 999, '>65'

2. now your select will look like this:

SELECT ag.title, Count(*)
FROM opinionvotes o, users u,  demographics  d
LEFT JOIN agegroups ag ON (year(now()) - d.birthyear - 1900 between ag.startage and ag.endage);
WHERE o.userID = u.userID
AND u.userID = d.userID
AND o.opinion_id = 232
GROUP BY ag.startage
0
 

Author Comment

by:rcbuchanan
ID: 11815859
wondeful! thanks!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now