January 16, 2007 at 9:12 am
I have the following table.
EmployeesChildren:
Employee,ChildName,ChildSex,ChildAge
Bob,Johnny,Male,11
Bob,Billy,Male,12
Bob,Jill,Female,9
Bob,Karen,Female,13
Maggie,Teddy,Male,2
Maggie,Mathew,Male,3
Maggie,Maria,Female,1
Maggie,Angie,Female,5
Now I need the following results(Oldest Male and Female child of each Employee).
Results Table:
Employee,ChildName,ChildSex,ChildAge
Bob,Billy,Male,12
Bob,Karen,Female,13
Maggie,Mathew,Male,3
Maggie,Angie,Female,5
Please note I know there is only 'Male' and 'Female' sex types in this example, but I really need it to be for any value in that field. i.e. If the value 'UNKNOWN' was in the field it would give me the oldest of that SexType as well.
Thanks,
Dean
January 16, 2007 at 9:26 am
Dean
Something like this? I haven't tested it.
John
SELECT e.Employee, a.ChildName, a.ChildSex, a.Age
FROM EmployeesChildren e JOIN
(SELECT Employee, ChildName, ChildSex, Max(ChildAge) as Age
FROM EmployeesChildren
GROUP BY Employee, ChildName, ChildSex) a
ON e.Employee = a.Employee
January 16, 2007 at 9:31 am
SELECT e.Employee, e.ChildName, e.ChildSex, e.Age
FROM EmployeesChildren e JOIN
(SELECT Employee, ChildSex, Max(ChildAge) as Age
FROM EmployeesChildren
GROUP BY Employee, ChildSex) a
ON e.Employee = a.Employee
and e.ChildSex = a.ChildSex
and e.Age = a.Age
Russel Loski, MCSE Business Intelligence, Data Platform
January 16, 2007 at 10:47 am
Thank you Russel Loski,
That worked perfect!!
thanks,
Dean
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply