I have table which holds birthdays and genders
SELECT `tblresultdatetime`, `tblresultbirthdate`, `tblgendertexten`
FROM `ci_wizard_results`
INNER JOIN ci_wizard_genders ON ci_wizard_results.tblresultgender = ci_wizard_genders.tblgenderid
Now I want to create a table like this:
So I want to create a table which points out the age groups etc.
I believe I first have to convert the dates to ages:
select *,year(`tblresultdatetime`)-year(`tblresultbirthdate`) - (right(`tblresultdatetime`,5) < right(`tblresultbirthdate`,5)) as age from `ci_wizard_results`
But after that, I am not sure how to continue. I believe I should use case:
select *,year(`tblresultdatetime`)-year(`tblresultbirthdate`) - (right(`tblresultdatetime`,5) < right(`tblresultbirthdate`,5)) as age,
count(case when age <= 30 and age> 39 then 1 end) as agegroup3039
from `ci_wizard_results`
But you can't use an alias in case, so I'm kinda stuck. Any suggestion how I could continue?
(My final goal is to display the data in a report via reportico)
Thanks!
Assuming that the age is calculate simply using
you can use case when and group by eg