具有动态列和JOINS 5+表的PIVOT [英] PIVOT with dynamic columns and JOINS 5+ tables
问题描述
This is an extension of a question posted by me earlier which can be found here : Previous Question
现在我还有2个表,这些表也连接到ProfileFan,以跟踪粉丝的活动(ProfileFan).
Now i have 2 more tables that are also connected to ProfileFan tracking the Activities of the Fan(ProfileFan).
Table Fan
-----------------------
| FanId | Name | Info |
-----------------------
| 17111 | Fan1 | Info1|
-----------------------
| 17112 | Fan2 | Info2|
-----------------------
Table ProfileFan m:1 Fan FanId(FK)
-----------------------------------
| Id | LinkedInProfileId | FanId |
-----------------------------------
| 1111 | 1 | 17111 |
---------------------------------
| 1112 | 2 | 17111 |
----------------------------------
| 1113 | 1 | 17112 |
----------------------------------
| 1114 | 2 | 17112 |
----------------------------------
Table LinkedInProfile
--------------------------
| Id | Name | Client |
--------------------------
| 1 | Linked1 | Client1|
--------------------------
| 2 | Linked2 | Client1|
--------------------------
Table FanActivity m:1 ProfileFan via ProfileFanId (FK)
------------------------------------------------
| Id | Created | ProfileFanId | ActivityId |
-------------------------------------------------
| 1 | 2019-01-05 | 17111 | 1 |
-------------------------------------------------
| 2 | 2019-01-05 | 17111 | 2 |
-------------------------------------------------
| 3 | 2019-01-05 | 17112 | 3 |
-------------------------------------------------
| 4 | 2019-01-05 | 17112 | 4 |
-------------------------------------------------
Table Activity Id(PK)
--------------------
| Id | Name |
---------------------
| 1 | ConAccepted |
---------------------
| 2 | Message |
---------------------
| 3 | LikesContent |
----------------------
| 4 | JoinsGroup |
----------------------
Table DeliveryActions m:1 ProfileFan via ProfileFanId (FK)
------------------------------------------------------
| Id | LoggedAt | ProfileFanId | DeliveryActionId |
-------------------------------------------------------
| 1 | 2019-01-05 | 17111 | 1 |
------------------------------------------------------
| 2 | 2019-01-05 | 17111 | 2 |
------------------------------------------------------
| 3 | 2019-01-05 | 17112 | 3 |
------------------------------------------------------
| 4 | 2019-01-05 | 17112 | 4 |
------------------------------------------------------
Table DeliveryAction Id(PK)
------------------------
| Id | Name |
------------------------
| 1 | M1 |
------------------------
| 2 | M2 |
------------------------
| 3 | M3 |
------------------------
| 4 | M4 |
------------------------
查询的输出应如下所示.
The output of the query should look like this.
-------------------------------------------------------------------------------------------------------------------------------------------------------
| Name | Info | LinkedInProfile1| LinkedInProfile2 | Client |ConAccepted | Message | LikesContent | JoinsGroup | M1 | M2 | M3 | M4 |
---------------------------------------------------------------------------------------------------------------------------------------------------
| Name1 | Info1| Linked1(1111) | Linked2(1112) | Client1 | 2019-01-05 | 2019-01-06 | 2019-01-07 | 2019-01-08 | 2019-01-09 | 2019-01-05 |2019-01-06 | 2019-01-07 |
---------------------------------------------------------------------------------------------------------------------------------------------------
| Name2 | Info2| Linked1(1113) | Linked2(1114) | Client1 |2019-01-05 | 2019-01-06 | 2019-01-07 | 2019-01-08 | 2019-01-09 | 2019-01-05 |2019-01-05 | 2019-01-0 |
---------------------------------------------------------------------------------------------------------------------------------------------------
我需要在查询中为单个风扇的数据插入一行,而不是为每一个连接插入多个风扇. 到目前为止,根据stackoverflow用户的建议,我已经解决了通过使用PIVOT和动态查询仅对LinkedInProfile做到这一点的解决方案.
I need to insert the data from a query for one fan in a single row instead multiple for every row that gives me the joins. So far as suggested by stackoverflow user i have come to solution to do it for only the LinkedInProfile by using PIVOT and dynamic query.
我也在这里 https://rextester.com/live/NYYK67222
我需要使用FanActivities和DeliveryActions表进行适当的联接,到目前为止,在联接之上,每个FAN都将数据放在一行中!
I need to make the appropriate joins with the FanActivities and DeliveryActions Table On top of the joins so far and put the data in a single row per FAN!
因此1个粉丝低于2个LinkedInProfiles->这将创建2个ProfileFansId.所有profileFans都具有许多FanActivity和许多DeliveryAction.
So 1 FAN is under 2 LinkedInProfiles -> This creates 2 ProfileFansId. All the profileFans have many FanActivities and Many DeliveryActions.
我知道我需要在这里使用带有动态列的PIVOT.但这对我来说是全新的.
I know that i need to use PIVOT with dynamic columns here. But its all new to me.
推荐答案
由于您期望的结果与rexter上的示例数据不同,因此我在FanActivities&中修改了一些数据DeliveryActions表.
Since your desired result is different from your sample data on rexter, I just modified few data in FanActivities & DeliveryActions tables.
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 |
+---------+-------+---------+-------------------+-------------------+-------------+------------+--------------+------------+------------+------------+------------+------------+
这篇关于具有动态列和JOINS 5+表的PIVOT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!