SSRS 2005:将列显示从项目列值/一行更改为其他内容?

发布于 2024-07-17 01:57:27 字数 375 浏览 8 评论 0原文

我有一个像这样的表/数据集:

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。

I have a table/dataset like:

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

I would like to display as

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

The idea is that the resulting printed page space is not wasted by printing one column on most of the page down but shortening the space to either a horizontal list or a columned result inside that field. What is the best way?

I would like to handle this is report designer as opposed to mucking with SQL results. I see several ways that gets close but not exact. I'm on SSRS 2005.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦初启 2024-07-24 01:57:27

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

Without using SQL to generate the CSV, then try a matrix control to change the rows into columns and groups on the date/training... but you won't have commas in between.

倾城花音 2024-07-24 01:57:27

任意标记您的三列 DateSessionParticipant 以及您的表格 MyTable

(我 不要认为您能够仅在报表设计器中执行此操作。 我认为最好的方法是编写一个标量值函数,它接受一个 datetime 和一个 varchar (或两个 varchar)作为两个输入并生成 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

(I'm arbitrarily labeling your three columns Date, Session, and Participant, and your table MyTable)

I don't think you're going to be able to do this solely in the report designer. The best way I see to do it is to write a scalar-valued function which takes a datetime and a varchar (or two varchars) as two inputs and produces a varchar output. The function would look something like this:

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))

Then, when you select from MyTable, do it with

SELECT Date, Session, dbo.MyFunction(Date, Session) As Participants GROUP BY Date, Session
蓝礼 2024-07-24 01:57:27

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

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

希望这可以帮助。

I wrote the following query to build a basic data set using a common table expression and then a grouping/concatenation operation to get the results I think you're looking for:

    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

This outputs the following:

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

You can format these results however you want in SSRS. No promises that this will run fast on a very large dataset due to the DISTINCT clause, but any grouping operation you do on a large dataset would be slow anyway. Using the FOR XML clause like this absolutely rules when it comes to concatenation.

Hope this helps.

故事还在继续 2024-07-24 01:57:27

在 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" 
   )

The usual way to do aggregate concatenation in SSRS is with custom code. See here for an example:

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

Here's the custom code in basic form:

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

Followed by this at the grouping level you want to display:

=RunningValue(
     Code.AggConcat(
         Fields!YourFieldToGroupBy.Value
       , Fields!YourFieldToConcat.Value
       )
   , Last
   , "YourGroupName" 
   )
忆伤 2024-07-24 01:57:27

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

I wound up using a SQL stuff/xml statement in the select portion of the t-sql with a join back to the overall select's data. Something like this: http://www.kodyaz.com/articles/concatenate-using-xml-path.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文