Question

Hey all, I'm having a difficult time wording this properly which is why im having trouble finding an answer online, so the best I can do is give an example. I have the following database table:

ACTORNAME                      SERIESNAME
------------------------------ ------------
baldwin                        found
baldwin                        lost
baldwin                        rocks
baldwin                        sienfield

costelo                        friends
costelo                        prince
costelo                        remember
denzel                         friends
denzel                         prince
denzel                         remember

fox                            found
fox                            friends
fox                            prince
lopez                          found
lopez                          friends
lopez                          prince
lopez                          remember

pitt                           er
pitt                           everybody
pitt                           friends
pitt                           heroes
pitt                           rocks
smith                          friends
smith                          prince
smith                          remember

And I would like to use a SELECT statement that would grab the actornames that play in all of the same series that smith plays in. So the resulting actornames should be:

costelo, denzel, and lopez

I'm not even sure which keyword to use. I was looking at the JOIN command and also tried MINUS, the closest I could get were actornames that play exactly in the same series that smith plays (in that situation, lopez is not included and is wrong)

Here is another explanation:

suppose Smith acts in movies X and Y.
Suppose also that actor 

A acts in movies X, Z
B acts in Y
C acts in X, Y, Z
D acts in X, Y

The answer to the query should be actors C and D. 

In other words, you have to return those actors whose set of movies contain those of actor Smith.

Looking for a push in the right direction, Tomek

Was it helpful?

Solution

Sorry. My original answer misunderstodd your intent. Try this instead:

select   t2.actorname, 
         count(t2.seriesname) 
from    mytable t1 
join    mytable t2 
on      t1.seriesname=t2.seriesname and t1.actorname='smith' and t2.actorname <> 'smith' group by t2.actorname 
having  count(t2.seriesname)=(select count(seriesname) from mytable where actorname='smith')

OTHER TIPS

SELECT DISTINCT ActorName 
    FROM dbo.MyTable
    WHERE SeriesName IN (SELECT SeriesName FROM dbo.MyTable WHERE ActorName = 'smith');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top