sql - remove duplicates/loops from graph data -
i have graph data arcs (consisting of start [s] , end [e]) of graph stored follows in relational table: if object_id('tempdb..#test') not null drop table #test; create table #test ( s nvarchar(1) ,e nvarchar(1) ); insert #test (s, e) values ('a', 'b'); insert #test (s, e) values ('b', 'a'); insert #test (s, e) values ('a', 'c'); so graph consists of these arcs: a -> b b -> a -> c i remove duplicates/cycles: -> b , b -> => -> b. possible? example ;with cte ( select * ,rn = row_number() on (partition case when s<e s+e else e+s end order s,e) #test ) delete cte rn>1 updated test s e b c wiki