views:

76

answers:

2

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

+2  A: 

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')
Matt Wrock
This returns all of that actors that are in any of the series that smith has played in . I need the actors that have played in ALL of the series that smith have played in.
Tomek
NO this wrong...this is not what is intended.It will give extra rows
Shankar Ramachandran
This statement returns only the actors that have played in ONLY the series that smith has played in and does not include that actors that have played in all the series smith played in and more, which should be included in the answer. (I added another explanation to my original question to clear things up a bit more, like I said this is very difficult to word properly)
Tomek
Are you sure? I tried it with the sample data above and got the three actors including Lopez.
martin clayton
@martin clayton I believe the part "having count(t2.seriesname)=(select count(seriesname) from mytable where actorname='smith')" limits the number of series that the actor being displayed has to 3 (the number of series smith has played in)
Tomek
actually your right, this does work. Thanks !
Tomek
so is the having statement equivalent to saying >= the number of series that smith played in?
Tomek
basically the statement is saying show me all actors and their matching seriesnames with smith where the number of seriesnames that match is equal to the total number of series that smith has been in.
Matt Wrock
A: 
SELECT DISTINCT ActorName 
    FROM dbo.MyTable
    WHERE SeriesName IN (SELECT SeriesName FROM dbo.MyTable WHERE ActorName = 'smith');
Damir Sudarevic