You can't do such logic in a single SQL Statement. If it was recursion then you could do WITH statement in SQL Server.