Connecting Tech Pros Worldwide Help | Site Map

tracing relationships

sd_bradford@hotmail.com
Guest
 
Posts: n/a
#1: Nov 13 '05
I am trying to solve a connectivity problem. A simplification:

tblTemp

Section Connected_Section id
A B 1
B C 2
C D 3
D E 4
G H 5
H I 6

What I want to do is set the id so that it groups the above into
similarly labelled blocks, as below:

Section Connected_Section id
A B 1
B C 1
C D 1
D E 1
G H 5
H I 5


I have used the following

UPDATE (tblTemp AS a INNER JOIN tblTemp AS b ON
b.section=a.connected_section) SET b.id=a.id

This works recursively upto a point and then fails to change some
records. Leaving the only option of rerunning the above query over and
over.

similarly I have also tried

UPDATE tblTemp AS a, tblTemp AS b SET b.id=a.id WHERE
b.section=a.connected_section

Is there anyway that I can make this follow through the whole table.
Some groups have about 120 connections so I don't want to have to loop
the query 120 times. Also the records are not in any order and sorting
will not put them into a correct order.

Please help

Si

Bas Cost Budde
Guest
 
Posts: n/a
#2: Nov 13 '05

re: tracing relationships


> I am trying to solve a connectivity problem. A simplification:

Glad to see this, I've been fighting a similar problem.
[color=blue]
> I have used the following
>
> UPDATE (tblTemp AS a INNER JOIN tblTemp AS b ON
> b.section=a.connected_section) SET b.id=a.id
>
> This works recursively upto a point and then fails to change some
> records. Leaving the only option of rerunning the above query over and
> over.[/color]

Which is what I ended up doing. You can examine the RecordsAffected
property of the querydef (if you put it in a query) or the database (if
you .Execute it) to see if you still have work left to do.
[color=blue]
> Is there anyway that I can make this follow through the whole table.
> Some groups have about 120 connections so I don't want to have to loop
> the query 120 times. Also the records are not in any order and sorting
> will not put them into a correct order.[/color]

You don't want to... There is another approach. Use an intermediate
table Progress(section, id)

For every record
is Section in Progress? ("left side")
yes-> change tblTemp.id into Progress.id
is there more than one matching record, with different ids, in
Progress? ("merger")
yes-> update tblTemp changing highest id into lowest
change Progress changing highest id into lowest
no -> is Connected_Section in Progress? ("right side")
yes-> change tblTemp.id into Progress.id
do same merger check as above
no -> add Section and Connected_Section to Progress

--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html
For human replies, replace the queue with a tea

sd_bradford@hotmail.com
Guest
 
Posts: n/a
#3: Nov 13 '05

re: tracing relationships


It works!

I wasn't sure about speed but it is really fast too.

Many thanks

Si

Closed Thread