Query for question


Please login or register to vote for this query.

(click on this box to dismiss)

Ukrainian Language

Q&A for linguists, teachers and students of the Ukrainian language

declare  @testTable TABLE(account int, Old_ID varchar(20), New_ID varchar(20) );
    --Two step loop
    INSERT @testTable VALUES(1, 'A', 'F')
    INSERT @testTable VALUES(1, 'F', 'A')
    --Three step loop
    INSERT @testTable VALUES(2, 'A', 'G')
    INSERT @testTable VALUES(2, 'G', 'X')
    INSERT @testTable VALUES(2, 'X', 'A')
    --Two step open chain
    INSERT @testTable VALUES(3, 'E', 'M')
    INSERT @testTable VALUES(3, 'M', 'C')

    --Three step open chain
    INSERT @testTable VALUES(4, 'A', 'T')
    INSERT @testTable VALUES(4, 'T', 'C')
    INSERT @testTable VALUES(4, 'C', 'Q')

    --Single element
    INSERT @testTable VALUES(5, 'F', 'F')

;with cteFilter (account, Old_ID, New_ID, First_ID, Last_ID, RecLevel) as
	select account, Old_ID, New_ID, Old_ID as First_ID, New_ID as Last_ID, 0 as RecLevel
	from @testTable
	union all
	select e.account, e.Old_ID, e.New_ID, d.First_ID, e.New_ID as Last_ID, d.RecLevel + 1
	from @testTable e
	inner join cteFilter as d 
		on	e.account = d.account 
			and e.Old_ID = d.New_id
			and e.Old_id <> d.first_ID
select account
from cteFilter
where First_id <> last_id
order by account

Enter Parameters

Switch to meta site
loading Hold tight while we fetch your results