Спасибо, ребята, за попытку помочь.
После целого дня я смог придумать что-то, что работает для меня. Возможно, это не лучший способ справиться с этим, но он работает.
Надеюсь, это может помочь кому-то еще.
DECLARE @pickedConstituenciesByUser varchar(8000) = 'Chief Financial Officers,Executive Directors,President'
DECLARE @query VARCHAR(8000)
DECLARE @column_headers VARCHAR(8000)
-- Creating the main temp table
CREATE TABLE #Main_Tbl
(
[ID] varchar(50),
column_value varchar(255),
Community varchar(255),
CitySize varchar(255),
CommunityCode varchar(255),
BillingStreet varchar(255),
BillingCity varchar(255),
BillingState varchar(255),
BillingPostalCode varchar(255),
BillingCountry varchar(255),
Title varchar(255),
RowNum int,
Column_header varchar(255)
)
-- Inserting intital data with constituencies and (contact names into column_value)
INSERT INTO #Main_Tbl
SELECT c.X18_Digit_Contact_ID__c,
c.[Name] column_value,
f.Community,
f.CitySize,
f.CommunityCode,
f.BillingStreet,
f.BillingCity,
f.BillingState,
f.BillingPostalCode,
f.BillingCountry,
ct.Constituency__c,
0 Rownum,
'' [Column_header]
FROM Federation f
JOIN Contact c ON f.Account_ID = c.AccountId
JOIN Constituency ct ON ct.Contact__c = c.X18_Digit_Contact_ID__c AND ct.Active__c = 1
-- Inserting intital data with affiliations and (contact names into column_value)
INSERT INTO #Main_Tbl
SELECT c.X18_Digit_Contact_ID__c,
c.[Name] column_value,
f.Community,
f.CitySize,
f.CommunityCode,
f.BillingStreet,
f.BillingCity,
f.BillingState,
f.BillingPostalCode,
f.BillingCountry,
a.npe5__Role__c,
0 Rownum,
'' [Column_header]
FROM Federation f
JOIN Contact c ON f.Account_ID = c.AccountId
JOIN Affiliation a ON c.X18_Digit_Contact_ID__c = a.npe5__Contact__c
AND
a.Affiliation_Type__c IN ('Professional','Lay')
AND
a.npe5__Status__c = 'Current'
-- Updating data with row number and column headers.
UPDATE A
SET A.RowNum = Z.RowNum, A.Column_header = z.Column_header
FROM #Main_Tbl A
JOIN
(
SELECT x.ID,
x.CommunityCode,
x.Community,
x.CitySize,
x.column_value,
x.Title,
ROW_NUMBER() OVER(PARTITION BY x.CommunityCode, x.Title ORDER BY x.column_value) RowNum,
x.Title + ' ' + cast(ROW_NUMBER() OVER(PARTITION BY x.CommunityCode, x.Title ORDER BY x.column_value) as varchar(10)) Column_header
FROM #Main_Tbl x
JOIN Contact c ON x.ID = c.X18_Digit_Contact_ID__c
WHERE Title IN (SELECT Value FROM fnc_Split_IDs(@pickedConstituenciesByUser, ','))
) Z ON A.ID = Z.ID
-- Inserting the same data - but this time replacing name with email into column_value
INSERT INTO #Main_Tbl
SELECT c.X18_Digit_Contact_ID__c,
c.Email column_value,
x.Community,
x.CitySize,
x.CommunityCode,
x.BillingStreet,
x.BillingCity,
x.BillingState,
x.BillingPostalCode,
x.BillingCountry,
x.Title,
0 Rownum,
x.Column_header + ' Email' [Column_header]
FROM #Main_Tbl x
JOIN Contact c ON x.ID = c.X18_Digit_Contact_ID__c
-- Inserting the same data again - but this time replacing email with phone into column_value
INSERT INTO #Main_Tbl
SELECT c.X18_Digit_Contact_ID__c,
c.Phone column_value,
x.Community,
x.CitySize,
x.CommunityCode,
x.BillingStreet,
x.BillingCity,
x.BillingState,
x.BillingPostalCode,
x.BillingCountry,
x.Title,
0 Rownum,
x.Column_header + ' ' + 'Phone' [Column_header]
FROM #Main_Tbl x
JOIN Contact c ON x.ID = c.X18_Digit_Contact_ID__c
WHERE Column_header NOT LIKE ('%Email%')
-- Inserting the same data again - but this time replacing phone with title into column_value
INSERT INTO #Main_Tbl
SELECT c.X18_Digit_Contact_ID__c,
a.npe5__Role__c column_value,
x.Community,
x.CitySize,
x.CommunityCode,
x.BillingStreet,
x.BillingCity,
x.BillingState,
x.BillingPostalCode,
x.BillingCountry,
x.Title,
0 Rownum,
x.Column_header + ' ' + 'Title' [Column_header]
FROM #Main_Tbl x
JOIN Contact c ON x.ID = c.X18_Digit_Contact_ID__c
JOIN Affiliation a ON x.ID = a.npe5__Contact__c AND a.npe5__Status__c = 'Current' AND a.npe5__Role__c <> 'Constituent'
WHERE Column_header NOT LIKE ('%Email%') AND Column_header NOT LIKE ('%Phone%')
-- creating a string with the proper column headers
SELECT @column_headers = STUFF
(
(
SELECT DISTINCT '],[' + TRIM([Column_header])
FROM #Main_Tbl
ORDER BY '],[' + TRIM([Column_header])
FOR XML PATH('')
), 1, 2, ''
) + ']'
-- replacing empty headers
SET @column_headers = Replace(@column_headers, '[],', '')
-- replacing email
SET @column_headers = Replace(@column_headers, '[Email],', '')
-- replacing phone
SET @column_headers = Replace(@column_headers, '[Phone],', '')
-- replacing title
SET @column_headers = Replace(@column_headers, ',[Title]', '')
-- Build main query
SET @query =
'SELECT * FROM ' +
'( ' +
'SELECT Community
, CitySize
, CommunityCode
, BillingStreet
, BillingCity
, BillingState
, BillingPostalCode
, BillingCountry
, column_value
, Column_header
FROM #Main_Tbl ' +
') AS t ' +
'PIVOT (MAX(column_value) FOR Column_header IN ('+ @column_headers +')) AS pvt ' +
'ORDER BY CitySize, Community'
-- Execute query
EXECUTE (@query)
DROP TABLE #Main_Tbl