[Solved] Sql Data Summery [closed]


use a recursive cte that starts with elements whose frompoint matches no topoint in a self join so as to identify the starting points. then eleminate the intermediate results by joining again and use only those data whose topoint match no frompoint in another self join.

with cte as (
    select r.name, r.frompoint, r.topoint
      from #t l
     right join 
           #t r
        on l.topoint = r.frompoint
       and l.name = r.name
     where l.name is null
     union all
    select l.name, l.frompoint, r.topoint
      from cte l
      join #t r
        on l.topoint = r.frompoint
       and l.name = r.name
)
select l.* 
  from cte l
  left join 
       #t r
    on r.name = l.name
   and r.frompoint = l.topoint
 where r.name is null

solved Sql Data Summery [closed]