我编写了以下查询以使用公用表表达式构建基本数据集,然后使用分组/连接操作来获得我认为您正在寻找的结果:
WITH temp AS (
SELECT CAST('1/1/2009' AS DATETIME) AS Date, 'Training 1' AS Session, 'Mike' AS Participant
UNION ALL
SELECT CAST('1/1/2009' AS DATETIME) AS Date, 'Training 1' AS Session, 'Bill' AS Participant
UNION ALL
SELECT CAST('1/1/2009' AS DATETIME) AS Date, 'Training 1' AS Session, 'Steve' AS Participant
UNION ALL
SELECT CAST('1/2/2009' AS DATETIME) AS Date, 'Training 2' AS Session, 'Steve' AS Participant
UNION ALL
SELECT CAST('1/2/2009' AS DATETIME) AS Date, 'Training 2' AS Session, 'Bill' AS Participant
UNION ALL
SELECT CAST('1/3/2009' AS DATETIME) AS Date, 'Training 3' AS Session, 'Mike' AS Participant
)
SELECT DISTINCT
Date,
Session,
(
SELECT STUFF(
(
SELECT
',' + CAST(Participant AS NVARCHAR(50)) + '' AS [text()]
FROM
temp b
WHERE
b.Date = a.Date AND
b.Session = a.Session
FOR
XML PATH('')
),
1,
1,
''
)
) AS Participants
FROM
temp a
这将输出以下内容:
Date Session Participants
2009-01-01 00:00:00.000 Training 1 Mike,Bill,Steve
2009-01-02 00:00:00.000 Training 2 Steve,Bill
2009-01-03 00:00:00.000 Training 3 Mike
您可以在 SSRS 中随意格式化这些结果。由于 DISTINCT 子句,无法保证这将在非常大的数据集上快速运行,但是您在大型数据集上执行的任何分组操作无论如何都会很慢。当涉及到连接时,使用这样的 FOR XML 子句绝对是规则。
希望这可以帮助。