Results 1 to 6 of 6
  1. #1
    racefan91 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    56

    Query to count matching records from a junction table


    I have a movie database and I want to count how many times 2 actors worked on a matching movie. I have a tblMovies, tblActors and tblMoviesActorsJunction. I'm not sure if there is a way to do some type of crosstab query or report where I can count how many times 2 actors have worked together. Example would be actors Robert Downey Jr and Chris Evans, movies would be Avengers, Captain America: Civil War and Avengers: Infinity War. So I want to count those and list it as them working together 3 times. Any suggestions on how to do this would be appreciated.

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Might need to see your Table structure to crack that, as I suspect it will require a number of sub-queries, to determine where any two actors have worked together to start with.
    Will probably need some form of cartesian join.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    racefan91 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    56
    My tables look like this:

    tblMovies has MovieID, MovieYear, MovieName
    tblActors has ActorID, ActorName

    I have a form where I enter the MovieYear and MovieName and it has a subform that adds the Actor. The subform is my junction table and has MovieID and ActorID.
    My junction table displays like this

    MovieID ActorID
    Avengers Robert Downey Jr
    Avengers Chris Evans
    Captain America: Civil War Robert Downey Jr
    Captain America: Civil War Chris Evans
    Avengers: Infinity War Robert Downey Jr
    Avengers: Infinity War Chris Evans

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2019
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Quote Originally Posted by racefan91 View Post
    The subform is my junction table and has MovieID and ActorID.
    A subform is a form. Not a table. It may be bound to a table/query. What is the name of the table/query that it is bound to.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  5. #5
    racefan91 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    56
    tblMoviesActorsJunction

  6. #6
    Join Date
    Apr 2017
    Posts
    1,679
    On fly:
    Code:
    SELECT MovieList.MovieID, m.MovieName, m.MovieYear, MovieList.MoviesCnt
    FROM
         (
         SELECT ma.MovieID, COUNT(ma.MovieID) As MoviesCnt
         FROM (tblMoviesActorsJunction ma INNER JOIN tblActors a1 ON a1.ActorID = ma.ActorID) INNER JOIN tblActors a2 ON a2.ActorID = ma.ActorID
         WHERE a1.ActorID = [FirstActorID] and a2.ActorID = [SecondActorID]
         GROUP BY ma.MovieID
         ) MovieList INNER JOIN tblMovies m ON m.MovieID = MovieList.MovieID
    This probably lists all movies those 2 actors participated in, without their names.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 08-17-2017, 12:44 PM
  2. Replies: 13
    Last Post: 05-10-2017, 12:43 AM
  3. Replies: 23
    Last Post: 01-27-2016, 01:42 PM
  4. Replies: 2
    Last Post: 12-22-2015, 09:09 PM
  5. Replies: 4
    Last Post: 02-04-2014, 10:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums