I have a table of data about people. The fields I'm working with are PersonID, FirstName, LastName, and SpouseID. PersonID is the primary key. If two people (in different rows) are married, they each have their spouses PersonID number in their SpouseID field. Single people don't have any entry in their SpouseID field. What I would like to do get a list of everyone from the table, with married couples listed together on one row. A sample of the data would be:
PersonID / FirstName / LastName / SpouseID
1 / John / Smith / 3
2 / Mark / Jones / 5
3 / Mary / Smith / 1
4 / Paula / Doe / null
5 / Jane / Jones / 2
6 / Harry / Johnson / null
What I would like the query to produce would be:
FirstName / LastName
John & Mary / Smith
Mark & Jane / Jones
Paula / Doe
Harry / Johnson
So far, this is what I've got - I'm still a long way off!
SELECT [tblMembers.FirstName] & " & " & [tblMembers_1.FirstName] AS CouplesNames, tblMembers.LastName
FROM tblMembers LEFT JOIN tblMembers AS tblMembers_1 ON tblMembers.PersonID = tblMembers_1.SpouseID;
PersonID / FirstName / LastName / SpouseID
1 / John / Smith / 3
2 / Mark / Jones / 5
3 / Mary / Smith / 1
4 / Paula / Doe / null
5 / Jane / Jones / 2
6 / Harry / Johnson / null
What I would like the query to produce would be:
FirstName / LastName
John & Mary / Smith
Mark & Jane / Jones
Paula / Doe
Harry / Johnson
So far, this is what I've got - I'm still a long way off!
SELECT [tblMembers.FirstName] & " & " & [tblMembers_1.FirstName] AS CouplesNames, tblMembers.LastName
FROM tblMembers LEFT JOIN tblMembers AS tblMembers_1 ON tblMembers.PersonID = tblMembers_1.SpouseID;