Поскольку желаемый результат отличается от данных примера на rexter, я только что изменил несколько данных в таблицах FanActivities & DeliveryActions.
CREATE TABLE #Fan (FanId INT, Name VARCHAR(100), Info VARCHAR(100));
INSERT INTO #Fan
VALUES (17111, 'Fan1', 'Info1'), (17112, 'Fan2', 'Info2');
CREATE TABLE #LinkedInProfile (Id INT, Name VARCHAR(100), Client VARCHAR(100));
INSERT INTO #LinkedInProfile
VALUES (1, 'Linked1', 'Client1'), (2, 'Linked2', 'Client1');
CREATE TABLE #ProfileFan (Id INT, ProfileId INT, FanId INT);
INSERT INTO #ProfileFan
VALUES (1111, 1, 17111), (1112, 2, 17111), (1113, 1, 17112), (1114, 2, 17112);
CREATE TABLE #Activities (Id INT, Name VARCHAR(100));
INSERT INTO #Activities
VALUES (1, 'ConAccepted'), (2, 'Message'), (3, 'LikesContent'), (4, 'JoinsGroup');
CREATE TABLE #FanActivities (Id INT, Created VARCHAR(100), ProfileFanId INT
, ActivityId INT);
INSERT INTO #FanActivities
VALUES
(1, '2019-01-05', 1111, 1), (1, '2019-01-05', 1111, 2)
, (1, '2019-01-05', 1112, 3), (1, '2019-01-05', 1111, 4)
, (1, '2019-01-05', 1113, 1), (1, '2019-01-05', 1113, 2)
, (1, '2019-01-05', 1114, 3), (1, '2019-01-05', 1113, 4);
CREATE TABLE #DeliveryAction (Id INT, Name VARCHAR(100));
INSERT INTO #DeliveryAction
VALUES (1, 'M1'), (2, 'M2'), (3, 'M3'), (4, 'M4');
CREATE TABLE #DeliveryActions (Id INT, Created VARCHAR(100), ProfileFanId INT
, ActivityId INT);
INSERT INTO #DeliveryActions
VALUES
(1, '2019-01-05', 1111, 1), (1, '2019-01-05', 1111, 2)
, (1, '2019-01-05', 1112, 3), (1, '2019-01-05', 1111, 4)
, (1, '2019-01-05', 1113, 1), (1, '2019-01-05', 1113, 2)
, (1, '2019-01-05', 1114, 3), (1, '2019-01-05', 1113, 4);
И ваш запрос будет
;WITH CTE AS(
SELECT F.FanId,F.Name AS FANNAME,F.Info
,CONCAT(lip.Name,'(',pf.Id,')') AS LINKEDPROFILE
,CONCAT('#LinkedInProfile',pf.ProfileId) as LipId
, LIP.Client
,FACT.Name AS FANACTIVITY, FA.Created FANACTIVITYDATE
,DA.Name AS DELVACTIVITY, DAS.Created DELVACTIVITYDATE
FROM #Fan F
LEFT JOIN #ProfileFan pf ON f.FanId = pf.FanId
LEFT JOIN #LinkedInProfile lip ON pf.ProfileId = lip.Id
LEFT JOIN #FanActivities FA ON PF.Id = FA.ProfileFanId
LEFT JOIN #ActivitieS FACT ON FACT.Id = FA.ActivityId
LEFT JOIN #DeliveryActions DAS ON PF.Id = DAS.ProfileFanId
LEFT JOIN #DeliveryAction DA ON DAS.ActivityId = DA.ID
)
SELECT FANNAME, Info, Client
,[#LinkedInProfile1],[#LinkedInProfile2]
,[ConAccepted],[Message],[LikesContent],[JoinsGroup]
,[M1],[M2],[M3],[M4] FROM (
SELECT FANNAME, Info, Client,KEYS,VALUE
FROM CTE
CROSS APPLY (
VALUES (LipId, LINKEDPROFILE)
, (FANACTIVITY,FANACTIVITYDATE)
,(DELVACTIVITY,DELVACTIVITYDATE)
)AS TAB(KEYS,VALUE)
)AS A
PIVOT
(
MAX(VALUE) FOR KEYS IN ([#LinkedInProfile1],[#LinkedInProfile2]
,[ConAccepted],[Message],[LikesContent],[JoinsGroup]
,[M1],[M2],[M3],[M4])
)PV
И результат, который я получил
+---------+-------+---------+-------------------+-------------------+-------------+------------+--------------+------------+------------+------------+------------+------------+
| FANNAME | Info | Client | #LinkedInProfile1 | #LinkedInProfile2 | ConAccepted | Message | LikesContent | JoinsGroup | M1 | M2 | M3 | M4 |
+---------+-------+---------+-------------------+-------------------+-------------+------------+--------------+------------+------------+------------+------------+------------+
| Fan1 | Info1 | Client1 | Linked1(1111) | Linked2(1112) | 2019-01-05 | 2019-01-05 | 2019-01-05 | 2019-01-05 | 2019-01-05 | 2019-01-05 | 2019-01-05 | 2019-01-05 |
| Fan2 | Info2 | Client1 | Linked1(1113) | Linked2(1114) | 2019-01-05 | 2019-01-05 | 2019-01-05 | 2019-01-05 | 2019-01-05 | 2019-01-05 | 2019-01-05 | 2019-01-05 |
+---------+-------+---------+-------------------+-------------------+-------------+------------+--------------+------------+------------+------------+------------+------------+