简体   繁体   English

根据参数显示按周或按月分组的SSRS报告数据

[英]Show data on SSRS report grouped week or month based on parameter

I'm trying to achieve the appropriate grouping to be shown on SSRS report which is driven by the 'weekly' or 'monthly' parameter defined in SSRS (not a argument for sproc). 我正在尝试在SSRS报告中显示适当的分组,该报告由SSRS定义的“每周”或“每月”参数驱动(不是针对sproc的参数)。 For that I'm using following in the Category Groups expression for the field called "Date" (format is '2014-03-01' as example): 为此,我在名为“Date”的字段的类别组表达式中使用了以下格式(格式为'2014-03-01'作为示例):

=IIF(
  Parameters!date_range.value="Weekly", 
  DATEPART("week", Fields!Date.Value),  
  DATEPART("month", Fields!Date.Value)
)

This results in the following exception: 这导致以下异常:

The Value expression for the field 'Date' contains an error: Argument 'DateValue' cannot be converted to type 'Date'. “Date”字段的Value表达式包含错误:参数“DateValue”无法转换为“Date”类型。 (rsRuntimeErrorInExpression). (rsRuntimeErrorInExpression)。 An error has occurred during report processing. 报告处理期间发生错误。 (rsProcessingAborted) (rsProcessingAborted)

Why? 为什么?

The easiest way to achieve this is to first of all write your query which pulls forward the results like this. 实现这一目标的最简单方法是首先编写查询,然后像这样提取结果。

SQL Server Query SQL Server查询

SELECT  DATEPART(MONTH, Date_Column) AS [Monthly]
       ,DATEPART(WEEK,  Date_Column) AS [Weekly]
       ,SUM(Some_Column)             AS Total
FROM Table_Name
GROUP BY DATEPART(MONTH, Date_Column) 
        ,DATEPART(WEEK,  Date_Column)

SSRS Report SSRS报告

Add a Matrix Data region. 添加Matrix数据区域。 Drag and drop Total column to DATA . Total列拖放到DATA

Create a Parameter say GROUP ON of Text type, and provide values 创建一个参数说明Text类型的GROUP ON ,并提供值

1) Weekly
2) Monthly

Now below in ROW GROUPS pane, right click the only visible Row Group and goto GROUP PROPERTIES In GROUP ON section put following expression. 现在,在ROW GROUPS窗格的下方,右键单击唯一可见的行组,并转到GROUP ON部分中的GROUP PROPERTIES放置以下表达式。

=IIF(Parameters!Groupby.Value = "Monthly", Fields!Monthly.Value, Fields!Weekly.Value)

Use the exactly same Expression on Data region ROWS section. 在数据区域ROWS部分使用完全相同的表达式。

For Column name you can use the following Expression... 对于列名,您可以使用以下表达式...

=IIF(Parameters!Groupby.Value = "Monthly", "Monthly", "Weekly")

and you are good to go. 你很高兴。

Important Note 重要的提示

SSRS is a cool tool for data presentation, not so cool when it comes to Data manipulation, to get better performance do all sorts of Data Manipulation closer to source (database, SQL Server). SSRS是一个很酷的数据表示工具,在数据操作方面并不那么酷,为了获得更好的性能,可以在更接近源(数据库,SQL Server)的情况下进行各种数据操作。

All of the presentation stuff should be handled on SSRS. 所有演示文稿都应该在SSRS上处理。

Try something like: 尝试类似的东西:

Format(Fields!date.Value,"yyyy-MM-dd")

Is this SQL Server 2014? 这是SQL Server 2014吗?

Link In SQL Server 2014, DATEPART implicitly casts string literals as a datetime2 type. 链接在SQL Server 2014中,DATEPART隐式地将字符串文字转换为datetime2类型。 This means that DATEPART does not support the format YDM when the date is passed as a string. 这意味着当日期作为字符串传递时,DATEPART不支持格式YDM。 You must explicitly cast the string to a datetime or smalldatetime type to use the YDM format. 必须将字符串显式转换为datetime或smalldatetime类型才能使用YDM格式。

So following from @nshah suggestion convert the expression to this: 因此,根据@nshah建议将表达式转换为:

=IIF(
  Parameters!date_range.value="Weekly", 
  DATEPART("week", format(Fields!Date.Value,"yyyy-MM-dd")),  
  DATEPART("month", format(Fields!Date.Value,"yyyy-MM-dd"))
)

I recommend using DATEPART, but I have discovered myself that DATEPART in SSRS does not always work as described in DATEPART (SSIS Expression) . 我建议使用DATEPART,但我发现自己SSRS中的DATEPART并不总是像DATEPART(SSIS Expression)中所描述的那样工作。 Here are some examples with specific syntax that worked for me: 以下是一些具有特定语法的示例,对我有用:

  • =DATEPART("yyyy", Fields!Date.Value) for year. = DATEPART(“yyyy”,Fields!Date.Value)一年。
  • =DATEPART("m", Fields!Date.Value) for month. =月份的DATEPART(“m”,Fields!Date.Value)。
  • =DATEPART("ww", Fields!Request_Date.Value) for week. = DATEPART(“ww”,Fields!Request_Date.Value)一周。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM