I am currently writing a query for the Campaign Members collection.
The collection contains many sets of CampaignMembers that are identical in every queried field except for CampaignId
When executing the SOQL query I'd like to be able to return one row per each of these sets of CampaignMembers. The resultant row would contain each of the fields that were identical across the set and one field contains an array of Campaign Ids.
Actual CampaignMembers Table
Id| ContactId | CampaignId 1 | 123 | 1 2 | 123 | 2 3 | 123 | 3 4 | 931 | 1
Rather than returning 4 separate objects with something like
query('SELECT ContactId, CampaignId FROM CampaignMember')
ContactId | CampaignId 123 | 1 123 | 2 123 | 3 931 | 1
I would ideally like to be able to return 2 objects
ContactId | CampaignId 123 | [1, 2, 3] 931 | 
Is it possible to manipulate the data like this within an SOQL query, or is it necessary to fetch each record individually and combine them afterwards?