Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 recursion repeats

Author  Topic 

jholovacs
Posting Yak Master

163 Posts

Posted - 2008-08-21 : 12:09:17
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:

  1. points table that contains my points (typically contacts with coordinates), a

  2. spans table that links two points together, an

  3. 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

  4. 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 c
inner join
feeds f
on
f.span_id = c.span_id
where
f.ps_id = 1 --this is the power supply I want to get the circuit for

union all

select
c.span_id,
c.point1,
c.point1_ends,
c.point2,
c.point2_ends,
t.span_count + 1
from
circuits c
inner 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

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-08-21 : 13:34:09
can you create some insert statement to [circuts] and [feeds] so we can try it with some sample data?

EDIT:
I think you problem is that you are trying to go in both directions simultaneously. You may need to work up then work down in another statement. Other wise you will need to include a column to track which direction you are moving and include that in your ON criteria.

Be One with the Optimizer
TG
Go to Top of Page

jholovacs
Posting Yak Master

163 Posts

Posted - 2008-08-22 : 08:38:26
yeah that's what I ended up doing. Thanks.

___________________________
Geek At Large
Go to Top of Page
   

- Advertisement -