I have to make an Recursive CTE to give me back all the actors who knew kevin bacon, to get better idea about my database I have Movies table and Actors and Movies2Actors table that contains only the actorid and the movieid, I need to get only the one who knew bacon through only two actors For example, you might wonder how Alfred Hitchcock can be connected to Kevin Bacon. One answer is that: Alfred Hitchcock was in Show Business at War (1943) with Orson Welles, and Orson Welles was in A Safe Place (1971) with Jack Nicholson, and Jack Nicholson was in A Few Good Men (1992) with Kevin Bacon! i am trying two different ways one is giving me an empty set, and the other is giving me this error message 'The full select of the recursive general table expression DBMASTER. BACON" must be the UNION link of two or more fullselects and must not include a column function, clause GROUP BY, HAVING or ORDER BY still contain an explicit join with a CLAUSE ON.'
WITH bacon (actorid, bacon_number) AS (
SELECT UNIQUE actorid, 0 FROM movies2actors
WHERE actorid = (SELECT actorid FROM actors WHERE name = 'Bacon, Kevin (I)') UNION ALL
SELECT movies2actors.actorid, bacon.bacon_number + 1
FROM movies2actors, bacon
WHERE movies2actors.actorid IN
(SELECT UNIQUE actorid FROM movies2actors WHERE movieid IN (SELECT UNIQUE movieid FROM movies2actors
WHERE actorid = (SELECT actorid FROM actors WHERE name = 'Bacon, Kevin (I)') ))
AND movies2actors.actorid <> (SELECT actorid FROM actors WHERE name = 'Bacon, Kevin (I)') AND bacon.bacon_number<2
)
SELECT bacon.actorid , bacon.bacon_number FROM bacon ;
WITH bacon (actorid,relationid, bacon_number) AS (
SELECT UNIQUE actorid, actorid ,0 FROM ACTORS
WHERE name = 'Bacon, Kevin (I)'
UNION ALL
SELECT ACTORS.actorid,bacon.relationid, bacon.bacon_number + 1
FROM ACTORS
JOIN bacon ON ACTORS.actorid = bacon.relationid
WHERE ACTORS.actorid IN
(SELECT UNIQUE actorid FROM movies2actors WHERE movieid IN (SELECT UNIQUE movieid FROM movies2actors
WHERE actorid = (SELECT actorid FROM actors WHERE name = 'Bacon, Kevin (I)') ))
AND ACTORS.actorid <> (SELECT actorid FROM actors WHERE name = 'Bacon, Kevin (I)') AND bacon.bacon_number<2
)
SELECT bacon.actorid , bacon.bacon_number FROM bacon ;