There are two tables, a Contact
Table and a RelationshipHistory
Table.
Contacts can have several types of relationships (Business, Individual, Volunteer, etc) which means they can have several references in the RelationshipHistory table over time. Each relationship row has a column indicating an event that the relationship is related to. So, for example, contact John Smith with ContactID 123 could have two rows in the RelationshipHistory Table like: 123, Individual, Fun Event and 123, Volunteer, Boring Event. The query that I need to perform is "Get all the contacts that have a relationship with event "Fun Event" and event "Boring Event".
I initially thought that a query like this would work:
SELECT DISTINCT LastName, FirstName
FROM Contacts
JOIN RelationshipHistory ON RelationshipHistory.ContactKey = Contacts.ContactGUID
WHERE RelationshipHistory.Event = 'Fun Event'
AND RelationshipHistory.Event = 'Boring Event'
But it doesn't return any results. When I took a second look at the query I realized that it is impossible because the comparison is happening on the rows and it doesn't make sense for a value in a column to equal two different values simultaneously.
The query seemed straightforward enough, but looking more closely at it, I'm not how to find the rows that have matching values on a column for two or more values.
Is there some way to use an inner select, or a join, or do I have to dump results into a temporary table...?