Sql PIVOT和字符串连接聚合

前端之家收集整理的这篇文章主要介绍了Sql PIVOT和字符串连接聚合前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想使用一个透视SQL查询来构造结果表,其中连接文本作为结果在数据透视表的DATA部分中.

即我使用简单的选择有以下结果:

+------------+-----------------+---------------+
| Event Name | Resource Type   | Resource Name |
+------------+-----------------+---------------+
| Event 1    | Resource Type 1 | Resource 1    |
| Event 1    | Resource Type 1 | Resource 2    |
| Event 1    | Resource Type 2 | Resource 3    |
| Event 1    | Resource Type 2 | Resource 4    |
| Event 1    | Resource Type 3 | Resource 5    |
| Event 1    | Resource Type 3 | Resource 6    |
| Event 1    | Resource Type 3 | Resource 7    |
| Event 1    | Resource Type 4 | Resource 8    |
| Event 2    | Resource Type 5 | Resource 1    |
| Event 2    | Resource Type 2 | Resource 3    |
| Event 2    | Resource Type 3 | Resource 11   |
| Event 2    | Resource Type 3 | Resource 12   |
| Event 2    | Resource Type 3 | Resource 13   |
| Event 2    | Resource Type 4 | Resource 14   |
| Event 2    | Resource Type 5 | Resource 9    |
| Event 2    | Resource Type 5 | Resource 16   |
+------------+-----------------+---------------+

我想构造一个结果查询,如下所示:

+---------------------+------------------------+------------------------+---------------------------------------+-----------------+-------------------------------------+
| Event/Resource Type | Resource Type 1        | Resource Type 2        | Resource Type 3                       | Resource Type 4 | Resource Type 5                     |
+---------------------+------------------------+------------------------+---------------------------------------+-----------------+-------------------------------------+
| Event 1             | Resource 1,Resource 2 | Resource 3,Resource 4 | Resource 5,Resource 6,Resource 7    | Resource 8      | NULL                                |
| Event 2             | NULL                   | Resource 3             | Resource 11,Resource 12,Resource 13 | Resource 14     | Resource 1,Resource 9,Resource 16 |
+---------------------+------------------------+------------------------+---------------------------------------+-----------------+-------------------------------------+

我知道如何在ms-sql中使用PIVOT语句,但是我不知道如何将资源名称聚合为每个资源类型的逗号分隔项目的连接.

P.S
我也可以使用SSRS 2008-R2使用Martix提供的解决方案,使用Report Builde 3将第一个表作为我的数据集,并创建一个将资源名称聚合成逗号分隔的字符串的矩阵.

解决方法

为了得到结果,首先应该将值连接成逗号分隔的列表.

我将使用CROSS APPLY和FOR XML PATH:

SELECT distinct e.[Event Name],e.[Resource Type],LEFT(r.ResourceName,LEN(r.ResourceName)-1) ResourceName
FROM yourtable e
CROSS APPLY
(
    SELECT r.[Resource Name] + ','
    FROM yourtable r
    where e.[Event Name] = r.[Event Name]
      and e.[Resource Type] = r.[Resource Type]
    FOR XML PATH('')
) r (ResourceName)

SQL Fiddle with Demo.给你结果:

| EVENT NAME |   RESOURCE TYPE |                          RESOURCENAME |
------------------------------------------------------------------------
|    Event 1 | Resource Type 1 |                Resource 1,Resource 2 |
|    Event 1 | Resource Type 2 |                Resource 3,Resource 4 |
|    Event 1 | Resource Type 3 |    Resource 5,Resource 7 |
|    Event 1 | Resource Type 4 |                            Resource 8 |
|    Event 2 | Resource Type 2 |                            Resource 3 |
|    Event 2 | Resource Type 3 | Resource 11,Resource 13 |
|    Event 2 | Resource Type 4 |                           Resource 14 |
|    Event 2 | Resource Type 5 |   Resource 1,Resource 16 |

然后你将把你的PIVOT应用到这个结果:

SELECT [Event Name],[Resource Type 1],[Resource Type 2],[Resource Type 3],[Resource Type 4],[Resource Type 5]
FROM
(
  SELECT distinct e.[Event Name],LEN(r.ResourceName)-1) ResourceName
  FROM yourtable e
  CROSS APPLY
  (
      SELECT r.[Resource Name] + ','
      FROM yourtable r
      where e.[Event Name] = r.[Event Name]
        and e.[Resource Type] = r.[Resource Type]
      FOR XML PATH('')
  ) r (ResourceName)
) src
pivot
(
  max(ResourceName)
  for [Resource Type] in ([Resource Type 1],[Resource Type 5])
) piv

请参阅SQL Fiddle with Demo.您的最终结果将是:

| EVENT NAME |        RESOURCE TYPE 1 |        RESOURCE TYPE 2 |                       RESOURCE TYPE 3 | RESOURCE TYPE 4 |                     RESOURCE TYPE 5 |
----------------------------------------------------------------------------------------------------------------------------------------------------------------
|    Event 1 | Resource 1,Resource 4 |    Resource 5,Resource 7 |      Resource 8 |                              (null) |
|    Event 2 |                 (null) |             Resource 3 | Resource 11,Resource 13 |     Resource 14 | Resource 1,Resource 16 |
原文链接:https://www.f2er.com/mssql/75480.html

猜你在找的MsSQL相关文章