【发布时间】:2019-08-26 05:31:01
【问题描述】:
这是我之前发布的问题的扩展,可以在这里找到:Previous Question
现在我还有 2 个表也连接到 ProfileFan 跟踪粉丝的活动 (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 |
------------------------
查询的输出应如下所示。
-------------------------------------------------------------------------------------------------------------------------------------------------------
| 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的解决方案。
我还在这里创建了一个模拟https://rextester.com/live/NYYK67222
我需要与 FanActivities 和 DeliveryActions 表进行适当的联接 在到目前为止的联接之上,并将数据放在每个 FAN 的一行中!
所以 1 个 FAN 在 2 个 LinkedInProfiles 下 -> 这会创建 2 个 ProfileFansId。所有 profileFans 都有很多 FanActivity 和很多 DeliveryAction。
我知道我需要在这里使用 PIVOT 和动态列。但这对我来说是全新的。
【问题讨论】:
-
你走了多远?您是否有一些 SQL 可以正确连接数据但没有正确输出?
-
@Xendy rextester.com/live/NYYK67222
-
SELECT CONCAT(lip.Name,'(',pf.Id,')') AS val, f.Name as 'Name', f.Info as 'Info', CONCAT('LinkedInProfile',pf.ProfileId) as LipId FROM Fan f LEFT JOIN ProfileFan pf ON f.FanId = pf.FanId LEFT JOIN LinkedInProfile lip ON pf.ProfileId = lip.Id这是我目前为止的情况 -
在您的示例数据 (Rexter) 中没有为 FAN2 记录的粉丝活动和交付活动,但它们的值出现在您想要的输出中。怎么样??
标签: sql sql-server pivot outer-join dynamic-columns