You don't have to use a recursive query with CTE, a subquery with a ranking window function will do just fine:The table I used looks like this:create table #Genealogy( Indi_ID int, Dad_ID int, Mom_ID int, Generation int)
And the query:select G.Indi_ID, G.Dad_ID, G.Mom_ID, G.Generation, t.SubGrpfrom #Genealogy G inner join ( select Dad_ID, Mom_ID, Generation, dense_rank() over(order by Dad_ID, Mom_ID, Generation) SubGrp from #Genealogy ) t on t.Dad_ID = G.Dad_ID and t.Mom_ID = G.Mom_ID and t.Generation = G.Generationgroup by G.Indi_ID, G.Dad_ID, G.Mom_ID, G.Generation, t.SubGrporder by t.SubGrp