I have 2 tables:
Attendee(AttendeeId integer PRIMARY KEY, LastName, FirstName, etc...)
Company(CompanyId integer PRIMARY KEY, CompanyName, etc ...)
and a many-to-many relation over them:
CompanyAttendeeRelation
(
CompanyId integer REFERENCES Company (CompanyId),
AttendeeId integer REFERENCES Attendee (AttendeeId),
PRIMARY KEY (CompanyId, AttendeeId)
);
So, from the above, a company can have more than one attendee and an attendee can be in more than one company.
However, I’ve tried, for instance:
SELECT (LastName || " " || FirstName) as AttendeeName,
CompanyAttendeeRelation.AttendeeId,
CompanyAttendeeRelation.CompanyId
FROM Attendee join CompanyAttendeeRelation on CompanyAttendeeRelation.AttendeeId = Attendee.AttendeeId join Company on CompanyAttendeeRelation.CompanyId = Company.CompanyId
GROUP BY LastName;
But I don’t get all companies for an attendee. That is, for instance, say an attendee with AttendeeId = 15 has a company with CompanyId = 20 and another with CompanyId = 100, I’d expect to get:
Doe John|15|20
Doe John|15|100
But I’m only getting:
Doe John|15|100
So I’m not sure if it is the JOIN that stops on the first match and the second one never gets computed or if somewhere duplicates (in this case for AttendeeId) are getting ignored.
Any ideas?
If you want all the combinations, why would you
GROUP BY? Try it without.Better yet, for this particular query, you don’t need to join
Companyat all. So try this: