In Mysql, I have a patient table with id, birth_year and gender. Next table is transactions in which I have id, patient_id [FK from patient] and checkup_date. I need to select rows such that I get number of checkups for all males and females in the range of ages.
How do I query to achieve the following tabular result ?
under 20, male: 10 times, female: 15 times
21 – 30, male: 20 times, female: 35 times
31 – 40, male: 30 times, female: 45 times
…. ……. ……… ……. …….. ……….
81 – 90, male: 10 times, female: 6 times
above 90, male : 0 times, female: 4 times
Thank you.
Best Answer
Two options.
a) Use sub query
Gives
b) Group by age first and then group in range in application.
When using ORMs it's not easy to do sub queries, so this might be a good idea.
Performance wise: Considering that average humans live for 100 years, there can be at max 200 records (100 male and 100 female) which can be easily looped in an application.
Gives