CROSS APPLY返回逗号分隔值以及GROUP BY

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CROSS APPLY返回逗号分隔值以及GROUP BY相关的知识,希望对你有一定的参考价值。

我正在处理一个查询,以返回一个报告,该报告将每个业务单位的日志记录分组,同时分配给每个业务单位的经理分隔的管理员列表,所以基本上有4个表。

[dbo].[Log]
[dbo].[BusinessUnit]
[dbo].[Manager]
[dbo].[BusinessUnitManager]

为了报告,BusinessUnit,Manager和BusinessUnitManager只是用于访问基本信息的表,我需要分组的真实信息包含在Log表中,我的最终结果集应该如下所示:

BusinessUnit.Name,
Log.Code,
Count(*) LogRecordsCount,
Emails (Comma separated list of Managers assigned to the Business Unit)

我一直在尝试使用CROSS APPLY获取逗号分隔列但无法使其正常工作,我当前的查询如下所示:

SELECT
L.BusinessUnit,
L.Code, 
COUNT(*) AS RecordsCount,
F.Emails
FROM [dbo].[LOG] AS L
INNER JOIN [dbo].[BusinessUnit] AS D ON L.BusinessUnit = D.ID
INNER JOIN [dbo].[BusinessUnitManager] AS DSO ON D.ID = DSO.BusinessUnit
CROSS APPLY
(
    SELECT STUFF((
    SELECT ',' + O.Email
    FROM [dbo].[Manager] AS O
    WHERE O.Id = DSO.IdManager
    ORDER BY O.Email ASC
    FOR XML PATH ('')), 1, 1, '')
) F(Emails)
GROUP BY L.BusinessUnit, L.Code, F.Emails

此查询的问题是返回重复的行,每个管理器电子邮件分配给业务单位一行,我的预期结果应该只有一行,其中一列包含所有管理员电子邮件的逗号分隔列表。

目前的结果:

+--------------+------+--------------+----------------------------+
| BusinessUnit | Code | RecordsCount |           Emails           |
+--------------+------+--------------+----------------------------+
| Americas     |   00 |           21 | americasmanager1@email.com |
| Americas     |   00 |           21 | americasmanager2@email.com |
| Asia         |   10 |            5 | asiamanager1@email.com     |
| Asia         |   10 |            5 | asiamanager2@email.com     |
+--------------+------+--------------+----------------------------+

预期结果:

+--------------+------+--------------+-------------------------------------------------------+
| BusinessUnit | Code | RecordsCount |                        Emails                         |
+--------------+------+--------------+-------------------------------------------------------+
| Americas     |   00 |           21 | americasmanager1@email.com,americasmanager2@email.com |
| Asia         |   10 |            5 | asiamanager1@email.com,asiamanager2@email.com         |
+--------------+------+--------------+-------------------------------------------------------+

我将感激你的帮助

答案

你很接近,只需两步即可:

1)将您的东西存储到临时表中

SELECT
L.BusinessUnit,
L.Code, 
O.Emails
into #tmp
FROM [dbo].[LOG] AS L
INNER JOIN [dbo].[BusinessUnit] AS D ON L.BusinessUnit = D.ID
INNER JOIN [dbo].[BusinessUnitManager] AS DSO ON D.ID = DSO.BusinessUnit
JOIN [Manager] AS O ON O.Id = DSO.IdManager
GROUP BY L.BusinessUnit, L.Code, O.Emails

2)使用外部申请自己在一栏中填写电子邮件

select BusinessUnit,Code, F.Emails from #tmp t 
outer apply( 
     SELECT STUFF(( SELECT ',' + inner.Email 
     FROM #tmp inner 
     WHERE t.Id = inner. 
     ORDER BY O.Email ASC 
     FOR XML PATH ('')), 1, 1, '') 
) F(Emails) 
group by BusinessUnit,Code
另一答案
SELECT
    L.BusinessUnit,
    L.Code, 
    COUNT(*) AS RecordsCount,
    (
        SELECT STUFF((
        SELECT ',' + O.Email
        FROM [dbo].[Manager] AS O
            INNER JOIN [dbo].[BusinessUnitManager] AS DSO ON O.Id = DSO.IdManager 
        WHERE 
            DSO.BusinessUnit = D.ID
        ORDER BY O.Email ASC
        FOR XML PATH ('')), 1, 1, '')
    ) as Emails
FROM [dbo].[LOG] AS L
    INNER JOIN [dbo].[BusinessUnit] AS D ON L.BusinessUnit = D.ID
GROUP BY L.BusinessUnit, L.Code, D.ID

以上是关于CROSS APPLY返回逗号分隔值以及GROUP BY的主要内容,如果未能解决你的问题,请参考以下文章

BigQuery 将列作为逗号分隔值

根据 GROUP_CONCAT 返回的逗号分隔列表中的 ID 提取更多记录

cross apply 和 outer apply

Redshift SQL 用 GROUP 逗号分隔字段

有没有办法将列的值聚合和/或连接为 Group By 查询中的逗号分隔项?

如何从mysql表中查找逗号分隔值