Skip to main content
deleted 52 characters in body
Source Link
joanolo
  • 13.6k
  • 8
  • 39
  • 66
SELECT 
    person.first_name, person.last_name, meeting.meeting_id, meeting.meeting_start, meeting.meeting_end
FROM 
    person 
    JOIN person_meeting ON person_meeting.meeting_id = person.person_id
    JOIN meeting ON meeting.meeting_id = person_meeting.meeting_id
WHERE     
    person.person_id IN 
    (SELECT  /* s2: Persons NOT having attended meetings INNOT (starting {1)} */
        person_id
    FROM 
        person_meeting
    WHERE 
        meeting_id IN 
        (
            (SELECT /* s1: Meetings NOT starting later than 20161225 = ({1)} */
                meeting_id
            FROM 
                meeting
            WHERE 
                meeting_start <= /* reversed! */  '2016-12-25 00:00:00'
            )
        )
   ) ;

Check it all at dbfiddle herehere

SELECT 
    person.first_name, person.last_name, meeting.meeting_id, meeting.meeting_start, meeting.meeting_end
FROM 
    person 
    JOIN person_meeting ON person_meeting.meeting_id = person.person_id
    JOIN meeting ON meeting.meeting_id = person_meeting.meeting_id
WHERE     
    person.person_id IN 
    (SELECT  /* s2: Persons NOT having attended meetings IN (1) */
        person_id
    FROM 
        person_meeting
    WHERE 
        meeting_id IN 
        (
            (SELECT /* s1: Meetings NOT starting later than 20161225 = (1) */
                meeting_id
            FROM 
                meeting
            WHERE 
                meeting_start <= /* reversed! */  '2016-12-25 00:00:00'
            )
        )
   ) ;

Check it all at dbfiddle here

SELECT 
    person.first_name, person.last_name, meeting.meeting_id, meeting.meeting_start, meeting.meeting_end
FROM 
    person 
    JOIN person_meeting ON person_meeting.meeting_id = person.person_id
    JOIN meeting ON meeting.meeting_id = person_meeting.meeting_id
WHERE     
    person.person_id IN 
    (SELECT  /* s2: Persons NOT having attended meetings NOT starting {1} */
        person_id
    FROM 
        person_meeting
    WHERE 
        meeting_id IN 
        (SELECT /* s1: Meetings NOT starting later than 20161225 {1} */
            meeting_id
        FROM 
            meeting
        WHERE 
            meeting_start <= /* reversed! */  '2016-12-25 00:00:00'
        )
   ) ;

Check it all at dbfiddle here

Source Link
joanolo
  • 13.6k
  • 8
  • 39
  • 66

Your SQL query has unbalanced parenthesis. You open 5 parenthesis, you close 4. This would solve it:

SELECT 
    person.first_name, person.last_name, meeting.meeting_id, meeting.meeting_start, meeting.meeting_end
FROM 
    person 
    INNER JOIN 
    (
        meeting
        INNER JOIN person_meeting 
             ON meeting.meeting_id = person_meeting.meeting_id
    )
    ON person.person_id = person_meeting.person_id
WHERE 
    person.person_id IN 
    (
        SELECT 
            person_id
        FROM 
            person_meeting
        WHERE 
            meeting_id IN 
            (
                (SELECT 
                     meeting_id
                 FROM 
                      meeting
                 WHERE 
                      meeting_id NOT IN 
                      (
                          SELECT 
                              meeting_id
                          FROM 
                              meeting
                          WHERE 
                               meeting_start > '2016-12-25 00:00:00'
                      )
                 )
           )
      ) ;

Plus: your query is more complicated that it needs be. You seem to look for all meetings attended by people who attended meetings NOT starting later than 20161225. This can be achieved with one less level of subquery, plus some simplification at the top JOINs:

SELECT 
    person.first_name, person.last_name, meeting.meeting_id, meeting.meeting_start, meeting.meeting_end
FROM 
    person 
    JOIN person_meeting ON person_meeting.meeting_id = person.person_id
    JOIN meeting ON meeting.meeting_id = person_meeting.meeting_id
WHERE     
    person.person_id IN 
    (SELECT  /* s2: Persons NOT having attended meetings IN (1) */
        person_id
    FROM 
        person_meeting
    WHERE 
        meeting_id IN 
        (
            (SELECT /* s1: Meetings NOT starting later than 20161225 = (1) */
                meeting_id
            FROM 
                meeting
            WHERE 
                meeting_start <= /* reversed! */  '2016-12-25 00:00:00'
            )
        )
   ) ;

Check it all at dbfiddle here