Home All Groups Group Topic Archive Search About

Six Degrees of Separation

Author
4 Nov 2005 11:07 PM
jvjean24601
I'm having trouble trying to code a SQL update statement that interestingly enough follows the scenario very similar to the 'Six Degrees of Separation' premise.

Consider that we have a table with the following data. We want to update the ACQUINTANCE column starting with KevinBacon. The only way I've been able to do this is by using a loop and going up the acquintance level one at a time. Is it at all possible to do this in one elegant and efficient SQL statement?

PERSON A    PERSON B    ACQUINTANCE?
John        Jane        ?
Josh        June        ?
Mark        June        ?
Mark        Gail        ?
Rick        Gail        ?
Rick        Kyra        ?
KevinBacon    Kyra        ?

The target result set is:

PERSON A    PERSON B    ACQUINTANCE?
John        Jane        No
Josh        June        Yes
Mark        June        Yes
Mark        Gail        Yes
Rick        Gail        Yes
Rick        Kyra        Yes
KevinBacon    Kyra        Yes -- jvjean24601 ------------------------------------------------------------------------ Posted via http://www.codecomments.com ------------------------------------------------------------------------

Author
5 Nov 2005 5:47 AM
Roji. P. Thomas
Have a look at

http://toponewithties.blogspot.com/2005/03/path-enumeration-using-prime-number.html

--
Roji. P. Thomas
Net Asset Management
http://toponewithties.blogspot.com


Show quote
"jvjean24601" <jvjean24601.1y0***@mail.codecomments.com> wrote in message
news:jvjean24601.1y00xs@mail.codecomments.com...
>
> I'm having trouble trying to code a SQL update statement that
> interestingly enough follows the scenario very similar to the 'Six
> Degrees of Separation' premise.
>
> Consider that we have a table with the following data. We want to
> update the ACQUINTANCE column starting with KevinBacon. The only way
> I've been able to do this is by using a loop and going up the
> acquintance level one at a time. Is it at all possible to do this in
> one elegant and efficient SQL statement?
>
> PERSON A PERSON B ACQUINTANCE?
> John Jane ?
> Josh June ?
> Mark June ?
> Mark Gail ?
> Rick Gail ?
> Rick Kyra ?
> KevinBacon Kyra ?
>
> The target result set is:
>
> PERSON A PERSON B ACQUINTANCE?
> John Jane No
> Josh June Yes
> Mark June Yes
> Mark Gail Yes
> Rick Gail Yes
> Rick Kyra Yes
> KevinBacon Kyra Yes
>
>
>
> --
> jvjean24601
> ------------------------------------------------------------------------
> Posted via http://www.codecomments.com
> ------------------------------------------------------------------------
>
Author
5 Nov 2005 8:19 PM
Chris2
"Roji. P. Thomas" <thomasr***@gmail.com> wrote in message
news:%23yOgOxc4FHA.744@TK2MSFTNGP10.phx.gbl...
> Have a look at
>
>
http://toponewithties.blogspot.com/2005/03/path-enumeration-using-prime-number.html
>
> --
> Roji. P. Thomas
> Net Asset Management
> http://toponewithties.blogspot.com

Roji. P. Thomas,

When I execute the script to build the Prime Numbers Table (found
on:
http://toponewithties.blogspot.com/2005/03/prime-numbers-table-and-insert-script.html),
it returns the error:

..Net SqlClient Data Provider: Msg 156, Level 15, State 1, Line 16
Incorrect syntax near the keyword 'BEGIN'.


The following line appears to have a missing element:

WHILE @cnt <>

What element is meant to come after "<>"?

Was it supposed to be 1000?


Sincerely,

Chris O.
Author
5 Nov 2005 6:21 AM
Sylvain Lafontaine
You have Recursive Queries with SQL-2005:
http://msdn.microsoft.com/msdnmag/issues/04/02/TSQLinYukon/

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: http://cerbermail.com/?QugbLEWINF


Show quote
"jvjean24601" <jvjean24601.1y0***@mail.codecomments.com> wrote in message
news:jvjean24601.1y00xs@mail.codecomments.com...
>
> I'm having trouble trying to code a SQL update statement that
> interestingly enough follows the scenario very similar to the 'Six
> Degrees of Separation' premise.
>
> Consider that we have a table with the following data. We want to
> update the ACQUINTANCE column starting with KevinBacon. The only way
> I've been able to do this is by using a loop and going up the
> acquintance level one at a time. Is it at all possible to do this in
> one elegant and efficient SQL statement?
>
> PERSON A PERSON B ACQUINTANCE?
> John Jane ?
> Josh June ?
> Mark June ?
> Mark Gail ?
> Rick Gail ?
> Rick Kyra ?
> KevinBacon Kyra ?
>
> The target result set is:
>
> PERSON A PERSON B ACQUINTANCE?
> John Jane No
> Josh June Yes
> Mark June Yes
> Mark Gail Yes
> Rick Gail Yes
> Rick Kyra Yes
> KevinBacon Kyra Yes
>
>
>
> --
> jvjean24601
> ------------------------------------------------------------------------
> Posted via http://www.codecomments.com
> ------------------------------------------------------------------------
>
Author
6 Nov 2005 2:11 AM
--CELKO--
If this is a tree, then look up "Nested Sets Model" and do  it in one
query.  The gimmick is building a forest and clustering it into trees.
if you started with ("John", "Jane"), then the results woudl be
reversed.   What you want is a column called "Acquaintance Group"
number or somesuch. 

Also get a copy of TREES & HIERARCHIES IN SQL.
Author
6 Nov 2005 4:43 PM
Tony Rogerson
The nested set method is:-

    1)    Difficult to maintain
    2)    Does not scale nor perform well out of the lab and in the real
world.

A better method is that developed by Itzik Ben Gan, information here:
http://www.windowsitpro.com/Articles/Index.cfm?ArticleID=8826&DisplayTab=Article

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials


Show quote
"--CELKO--" <jcelko***@earthlink.net> wrote in message
news:1131243091.719499.156640@g47g2000cwa.googlegroups.com...
> If this is a tree, then look up "Nested Sets Model" and do  it in one
> query.  The gimmick is building a forest and clustering it into trees.
> if you started with ("John", "Jane"), then the results woudl be
> reversed.   What you want is a column called "Acquaintance Group"
> number or somesuch.
>
> Also get a copy of TREES & HIERARCHIES IN SQL.
>

AddThis Social Bookmark Button