Getting oldest child of parents and if, get only one of any of the twin/triplet
Hi. I had a problem getting the oldest child of a parent in our database in school. I was able to get the answer with this query.


Select [Student Number],[Last Name],[FirstName Complete],[Home Phone],[Mother],[Father],[Grade Level],[DOB]
from  [student]  st 
where exists (Select 1 from (Select MAX([Grade Level]) as [maxGrade], [Home Phone]  from ['student] group by [Home Phone]) as maxx
where maxx.[Home Phone] =st.[Home Phone]
and st.[Grade Level] = maxx.[maxGrade])
order by [Home Phone]



Now, when I looked at the record, I saw that there are twins and triplets. It will be hard to distinguish the oldest of them since hey have same birthday. So I now have to get any of the twins/triplets. Can you help me how to get only one from that twin/triplets?

July 30th, 2015 3:22am

Use RANK function to order the children in terms of their Grade Level and  it would be something like this

 EG: SELECt Customer_Details.*,RANK()OVER(ORDER BY Customer_Details.CustAge) AS ROWN FROM Customer_Details

AND then filter based on Rank = 1

  
Free Windows Admin Tool Kit Click here and download it now
July 30th, 2015 3:52am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics