1

我有一个表/数据集,例如:

1/1/2009 | Training 1 | Mike
1/1/2009 | Training 1 | Bill
1/1/2009 | Training 1 | Steve

我想显示为

1/1/2009 | Training 1 
Mike, Bill, Steve

这个想法是,打印的页面空间不会浪费在大部分页面上打印一列,而是将空间缩短为水平列表或该字段内的列式结果。什么是最好的方法?

我想处理这个是报表设计器,而不是处理 SQL 结果。我看到了几种接近但不准确的方法。我在 SSRS 2005 上。

4

5 回答 5

0

不使用 SQL 生成 CSV,然后尝试使用矩阵控件将行更改为日期/培训上的列和组......但您之间不会有逗号。

于 2009-04-17T20:04:41.127 回答
0

(我随意标记你的三列Date,,SessionParticipant,和你的表MyTable

我认为您无法仅在报表设计器中执行此操作。我认为最好的方法是编写一个标量值函数,它将 adatetime和 a varchar(或两个varchars)作为两个输入并产生一个varchar输出。该函数看起来像这样:

Declare @Names varchar(MAX), @Participant varchar(MAX)
Set @Names = ''
Declare NameCursor Insensitive cursor For 
    SELECT Participant FROM MyTable WHERE Date = @Date AND Session = @Session
Open NameCursor
Fetch Next From NameCursor Into @Participant
While @@FETCH_STATUS=0
Begin
    Select @Names = @Names + ', ' + @Participant
    Fetch Next From NameCursor Into @Participant
End
Close NameCursor
Deallocate NameCursor
Return SUBSTRING(@Names, 3, LEN(@Names))

然后,当您从MyTable中选择时,请使用

SELECT Date, Session, dbo.MyFunction(Date, Session) As Participants GROUP BY Date, Session
于 2009-05-20T20:54:12.330 回答
0

我编写了以下查询以使用公用表表达式构建基本数据集,然后使用分组/连接操作来获得我认为您正在寻找的结果:

    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 子句绝对是规则。

希望这可以帮助。

于 2009-06-10T16:46:30.367 回答
0

在 SSRS 中进行聚合连接的常用方法是使用自定义代码。请参阅此处的示例:

http://blogs.msdn.com/suryaj/archive/2007/08/11/string-aggregation.aspx

这是基本形式的自定义代码:

Private CurrGroupBy As String = String.Empty
Private ConcatVal As String = String.Empty
Public Function AggConcat(GroupBy as String, ElementVal as String) as String
    If CurrGroupBy = GroupBy Then
        ConcatVal = ConcatVal & ", " & ElementVal 
    Else
        CurrGroupBy = GroupBy 
        ConcatVal = ElementVal 
    End If
    Return ConcatVal 
End Function

然后在您要显示的分组级别:

=RunningValue(
     Code.AggConcat(
         Fields!YourFieldToGroupBy.Value
       , Fields!YourFieldToConcat.Value
       )
   , Last
   , "YourGroupName" 
   )
于 2009-06-10T19:47:47.147 回答
0

我最终在 t-sql 的选择部分中使用了 SQL stuff/xml 语句,并连接回了整个选择的数据。像这样的东西:http ://www.kodyaz.com/articles/concatenate-using-xml-path.aspx

于 2009-09-29T17:11:35.370 回答