I have a situation where I am trying to trace electrical connectivity by connective endpoint structures using recursion.one (or several) of my endpoints will have a power feed connected to it, and this is where I need to start my trace, as the point of this exercise is to verify that all the endpoints are supplied with power from a specific source.Ultimately, I have a: - points table that contains my points (typically contacts with coordinates), a
- spans table that links two points together, an
- endpoints table that references a span and a point, indicating that while there may be another span connected to that same contact point, it is not electrically connected to the referenced span, and a
- feeds table that contains the power supply devices and references a span.
Got all that?I normalized my data into a table like so:table circuits ( span_id int primary key, point1 int, point1_ends bit, point2 int, point2_ends bit )
I also organized the data so that the point1 reference value is always less than the point2 reference value.Now I attempt to write a recursive query to trace the circuit in both directions, stopping when the cicuit ends:;with trace as(select distinct c.span_id, c.point1, c.point1_ends, c.point2, c.point2_ends, 0 as 'span_count'from circuits cinner join feeds f on f.span_id = c.span_idwhere f.ps_id = 1 --this is the power supply I want to get the circuit forunion allselect c.span_id, c.point1, c.point1_ends, c.point2, c.point2_ends, t.span_count + 1from circuits cinner join trace t on (c.point1 = t.point1 and c.span_id != t.span_id and t.point1_ends != 1) or (c.point1 = t.point2 and c.span_id != t.span_id and t.point2_ends != 1) or (c.point2 = t.point1 and c.span_id != t.span_id and t.point1_ends != 1) or (c.point2 = t.point2 and c.span_id != t.span_id and t.point2_ends != 1)where t.span_count < 20)select * from trace;
Well, this cycles round and round till my span count grows too big. I really want to make sure that a span is not referenced more than once, but I can't use a DISTINCT, or a subquery in the recursive part of my query to filter out repeating spans.This HAS to be possible. It's probably very simple, but it completely eludes me at the moment. Help is appreciated.___________________________Geek At Large