Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
ann
Posting Yak Master
220 Posts |
Posted - 2007-07-13 : 13:54:46
|
Requirement:create a report that produces statistcal data based on age groups and gender.Query1: The agegroups need to be show a count of agegroups (see results) based on dateOfVisit - DateOfBirth to determine ageQuery2: The gender needs to show how many (count) persons were male and how many femaleexample tables:tblVisits:VisitID PersonID DateOfVisit 1 2 12\1\062 3 6\2\073 4 4\3\074 2 2\2\075 5 2\3\07tblPersonPersonID DateOfBirth gender2 11\1\02 Male3 5\5\84 Female4 2\12\32 Male5 1\8\82 FemaleResults AgeCategory:AgeUnder5 5-11yrs 12-19 20-64 65Over 1 1 0 2 1 Results GenderCategory:GenderMaleCount GenderFemaleCount 2 2I have no idea on how to do this - can anyone help pls? Thanks |
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-07-13 : 14:12:33
|
sounds like a homework question? Can you provide what you have tried so far? and what errors, if any, you have seen so far.. so we can guide you better..Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
ann
Posting Yak Master
220 Posts |
Posted - 2007-07-13 : 14:47:28
|
I don't know what you mean by "homework", but anyway, I started with query 2 (gender) since I thought it would be easier ... this is what I got, but it's not right:SELECT count(p.Gender) as GenderFROM tblPerson as pLeft Join tblVisits as ton p.patient_id = t.patient_idGroup By p.GenderResults:5282985624As you can see, I have 3 rows, but I should have only 2 genders! LOL! |
 |
|
ann
Posting Yak Master
220 Posts |
Posted - 2007-07-13 : 15:13:09
|
ok - I have query 2 figured out:SELECT gender, count(p.Gender) as genderCountFROM tblPerson as pLeft Join tblPerson as ton p.patient_id = t.patient_idwhere gender != 'U'Group By p.GenderIt seems they had another "gender" type in there....So for query 1 (grouping by age), how do I do a calculation to determine age? Can anyone point me in the right direction - thanks |
 |
|
ann
Posting Yak Master
220 Posts |
Posted - 2007-07-13 : 16:16:57
|
on query 2 - determining age - this is what I have so far:SELECT DATEDIFF(day, DateOfBirth, dateOfVisit)/365 AS ageFROM tblPersonLeft Join tblVisitson tblPerson.patientid = tblVisits.patientidbut I need to do a count on the age groups based on different parameters (example under5, 65+, etc.,)Can anyone help with that? |
 |
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-07-13 : 17:29:37
|
This function does an age calculation taking things like leap year into account.Age Function F_AGE_IN_YEARS:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=74462select [No Visits] = sum(case when a.VisitID is null then 1 else 0 end), [Age Under5] = sum(case when a.AgeAtVisit < 5 then 1 else 0 end), [Age 5-11] = sum(case when a.AgeAtVisit between 5 and 10 then 1 else 0 end), [Age 12-19] = sum(case when a.AgeAtVisit between 12 and 19 then 1 else 0 end), [Age 20-64] = sum(case when a.AgeAtVisit between 20 and 64 then 1 else 0 end), [Age 65orOver] = sum(case when a.AgeAtVisit > 64 then 1 else 0 end)from ( select AgeAtVisit = dbo.F_AGE_IN_YEARS( p.DateofBirth, t.DateOfVisit), t.VisitID from tblPerson as p Left Join tblVisits as t on p.patient_id = t.patient_id ) a CODO ERGO SUM |
 |
|
|
|
|
|
|