Solved

MYSQL Select Statement to Return Groups of Ages?

Posted on 2004-08-16
2
744 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (http://php.net/manual/en/function.mysql-connect.php).  Today (http://php.net/manual/en/migration70.removed-exts-sapis.php) you would not use MySQL…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

860 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