如何在SQL Server Reporting Services中使用表达式来创建有效的报告

介绍 (Introduction)

As many of you by now know, I am a fan of utilizing expressions within Reporting Services reports to add additional flexibility. Recently I received a client request to create a stacked bar report which in turn would provide access to the underlying data the made up the bars on the chart. My client sells two articles: “Whigs” and “Tories”. Many people like to “buy” them.The idea is to display the aggregated data as may be seen below (gray black and turquoise) and depending on which bar and color is selected( clicked upon), to drill down and to display the underlying detailed data (see below).

就像现在许多人所知道的那样,我热衷于利用Reporting Services报表中的表达式来增加额外的灵活性。 最近,我收到一个客户请求,以创建一个堆叠的条形图报告,该报告将依次提供对构成条形图上的基础数据的访问。 我的客户销售两篇文章:“辉格”和“故事”。 许多人喜欢“购买”它们。其想法是显示汇总的数据(如下所示)(灰黑色和青绿色),并根据选择的条形和颜色(单击),向下钻取并显示基础详细数据(见下文)。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

In today’s “fire side chat”, we are going discuss how the report may be constructed in an efficient and effective manner.

在今天的“边火聊天”中,我们将讨论如何高效有效地构建报告。

Let’s get started!

让我们开始吧!

入门 (Getting started)

We begin our journey by having a quick look at the raw data which has been loaded into our SQLShack database.

我们快速浏览一下已加载到我们SQLShack数据库中的原始数据,开始我们的旅程。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

The contents of the Whig table may be seen above.

Whig表的内容可以在上方看到。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

The contents of the Tories table may be seen above.

Tories表的内容可以在上方看到。

Now that we have had a quick look at the data behind our report, it is now time to begin constructing our client’s report.

现在,我们已经快速浏览了报告背后的数据,现在该开始构建客户报告了。

创建客户报告 (Creating our client’s report)

Opening Visual Studio 2015 or SQL Server Data Tools 2010 or greater, we create a new Reporting Services project.

打开Visual Studio 2015或SQL Server Data Tools 2010或更高版本,我们创建一个新的Reporting Services项目。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

We give our project a name and click “OK” to create the project.

我们给我们的项目起一个名字,然后单击“确定”以创建该项目。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

We find ourselves on our drawing surface.

我们发现自己在图纸表面上。

Should setting up a SQL Server Reporting Services project not be your forte, then please do a look at a SQL Shack article entitled “Now you see it, Now you don’t” where the process is described in detail. The link to this article may be seen below:

如果您不是要设置SQL Server Reporting Services项目,那么请仔细阅读SQL Shack文章,标题为“现在看到它,现在您不知道”,其中详细描述了该过程。 可以在下面看到本文的链接:

Now you see it, now you don’t

现在您看到了,现在您没有了

Having arrived on our drawing surface, our first task is to create a data connection to the SQLShack database.

到达图纸表面后,我们的首要任务是创建与SQLShack数据库的数据连接。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

By right-clicking upon the “Shared Data Source” tab, we select “Add New Data Source” (see above).

通过右键单击“共享数据源”选项卡,我们选择“添加新数据源”(请参见上文)。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

The “Shared Data Source Properties” dialogue box appears. We give our “Shared data source” a name and set the connection string to point to the SQLShack database (see above). We click “OK” to leave the “Shared Data Source Properties” screen.

出现“共享数据源属性”对话框。 我们为“共享数据源”命名,并设置连接字符串以指向SQLShack数据库(请参见上文)。 我们单击“确定”离开“共享数据源属性”屏幕。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

We find ourselves back upon the design surface. We right click on the “Reports” tab to bring up the context menu (see above). We select “Add” and “New Item”.

我们发现自己回到了设计表面。 我们右键单击“报告”选项卡以打开上下文菜单(请参见上文)。 我们选择“添加”和“新项目”。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

We select “Report” from the “Add New Item” menu and give our report a name (see above).

我们从“添加新项”菜单中选择“报告”,并给我们的报告起一个名字(见上文)。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

Once again, we find ourselves on our drawing surface (see above).

再次,我们发现自己在绘图表面上(参见上文)。

Our next task is to create a local data source and a local data set which will provide our report with the necessary data. As I have discussed in past “get togethers”, we tend to utilize local data sources and local datasets (in our reports) as the two permit an added degree of customization, exclusive to that report only.

我们的下一个任务是创建一个本地数据源和一个本地数据集,它们将为我们的报告提供必要的数据。 正如我在过去的“聚在一起”中所讨论的那样,我们倾向于利用本地数据源和本地数据集(在我们的报告中),因为两者允许增加程度的自定义,仅针对该报告。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

We right click upon the “Datasets” tab and select “Add Dataset” from the context menu (see above).

我们右键单击“数据集”选项卡,然后从上下文菜单中选择“添加数据集”(请参见上文)。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

The “Dataset Properties” dialogue box opens. We give our proposed dataset a name and we select the “Use a dataset embedded in my report” option (see above). At this point, we must create a new “local data source” thus we click the “New” button (see above).

“数据集属性”对话框打开。 我们为提议的数据集命名,然后选择“使用嵌入到我的报告中的数据集”选项(请参见上文)。 此时,我们必须创建一个新的“本地数据源”,因此我们单击“新建”按钮(参见上文)。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

The “Data Source Properties” dialogue box appears. We give our local data source a name and link it to the “Shared Data Source” that we created above (see screen shot above). We click “OK” to continue.

出现“数据源属性”对话框。 我们给本地数据源命名,并将其链接到上面创建的“共享数据源”(请参见上面的屏幕截图)。 我们单击“确定”继续。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

We find ourselves back on the “Dataset Properties” dialogue page. We select “Text” for the query type and add our code (see above). Now the “eagle-eyed” reader will suggest the utilization of a stored procedure instead of the text code which I just used. I totally agree and in a true life scenario, I would utilize a stored procedure so as well.

我们回到“数据集属性”对话框页面。 我们为查询类型选择“文本”并添加我们的代码(请参见上文)。 现在,“老鹰眼”的读者将建议使用存储过程,而不是我刚才使用的文本代码。 我完全同意,在现实生活中,我也会使用存储过程。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

We click “Refresh Fields” and switch to the “Fields” tab.

我们单击“刷新字段”,然后切换到“字段”选项卡。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

Having clicked the “Fields” tab, we note that fields that will be utilized by our “Bar Chart” report (see above). We click “OK” to leave the “Dataset Properties” dialogue box.

点击“字段”标签后,我们注意到“条形图”报告将使用这些字段(请参见上文)。 我们单击“确定”离开“数据集属性”对话框。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

Once again we find ourselves back upon our drawing surface and we note the dataset that we just created, is now visible (see above and to the left).

我们再次回到绘图表面,并注意到刚才创建的数据集现在可见(请参见上方和左侧)。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

Opening the “Toolbox” we now opt to place a “Chart” on our drawing surface (see above).

现在,我们打开“工具箱”,选择将“图表”放置在工程图面上(参见上文)。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

The “Select Chart Type” dialogue box appears. We select a “Stacked Column” column chart (see above).

出现“选择图表类型”对话框。 我们选择一个“堆积柱”柱形图(见上文)。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

The “Stacked Column” chart has been placed upon our drawing surface(see above).

“堆积柱”图已放置在我们的工程图面上(请参见上文)。

We now open the chart’s properties box by clicking on the chart and pressing F4 and set the “DataSetName” to the name of the local data set that we created above (see screenshot above).

现在,我们通过单击图表并按F4来打开图表的属性框,并将“ DataSetName”设置为我们在上面创建的本地数据集的名称(请参见上面的屏幕截图)。

We now click on the chart to bring up the “Chart Data” box. We set the “Values” tab to sum the revenue, the “Categories Group” to the Year and last but not least, the “Series Group” to the “Klass” (which consists of two values, “Whigs” and “Tories”).

现在,我们单击图表以弹出“图表数据”框。 我们将“值”标签设置为汇总收入,将“类别组”汇总为年份,最后但并非最不重要的是,将“系列组”汇总为“ Klass”(由两个值组成:“辉格”和“ Tory” )。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

Clicking the preview tab (see above) we run our report.

单击预览选项卡(见上文),我们运行报告。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

We now do a tad of cosmetic change to the report by formatting the axes and by adding data labels as may be seen above.

现在,我们通过格式化轴和添加数据标签(如上所示)对报告进行了一些修饰。

创建我们的子报告 (Creating our Sub Reports)

As we discussed in the introduction, the client had requested to view the raw data that made up the different segments of the stacked bar chart. The client and I had agreed that he would click on either the black or turquoise portion of the chart, for any year and the underlying data for that year would be displayed in a separate report.

正如我们在简介中讨论的那样,客户已请求查看构成堆叠条形图不同部分的原始数据。 客户和我已经同意,他将单击任何年份图表的黑色或青绿色部分,并且该年份的基础数据将显示在单独的报告中。

Our current task is to make this happen.

我们当前的任务是实现这一目标。

内部管理工作室 (Inside Management Studio )

Opening SQL Server Management Studio, we shall create two stored procedures, which will be called when the end user clicks upon one of the segments of the vertical bar chart. If the end user clicks upon the “black” portion then “GetWhigs” stored procedure will be executed. If the user clicks upon the “turquoise” portion of the chart then the “GetTories” stored procedure will be executed.

打开SQL Server Management Studio,我们将创建两个存储过程,当最终用户单击垂直条形图的某一段时,将调用这些存储过程。 如果最终用户单击“黑色”部分,则将执行“ GetWhigs”存储过程。 如果用户单击图表的“绿松石”部分,则将执行“ GetTories”存储过程。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

The “GetTories” stored procedure may be seen above.

可以在上面看到“ GetTories”存储过程。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

The “GetWhigs” store procedure may be seen above.

可以在上方看到“ GetWhigs”存储过程。

Before leaving management studio, we note that a parameter called “@yearr” has been added to both query predicates. This value will be passed to the query/stored procedure at run time.

在离开Management Studio之前,我们注意到在两个查询谓词中都添加了一个名为“ @yearr”的参数。 该值将在运行时传递给查询/存储过程。

We shall now leave SQL Server Management Studio and begin the construction of our two sub reports.

现在,我们将离开SQL Server Management Studio,并开始构建两个子报表。

创建子报告 (Creating the sub reports)

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

In a similar fashion to the report that we created above, we create a Whig and a Tory sub report. We shall be designing the Whig report. It is left to the reader to design the report for the Tories.

以与上面创建的报告类似的方式,我们创建了辉格图和Tory子报告。 我们将设计辉格报告。 留给读者来设计保守党的报告。

Once again, should not be familiar with the process of configuring a Reporting Services Matrix / Tablix please do have a look at a SQL Shack article entitled “Queries, matrices, pivot tables and more” where the process is described in detail. The link to this article may be seen below:

再说一次,不应该熟悉配置Reporting Services Matrix / Tablix的过程,请查看一下SQL Shack的标题为“查询,矩阵,数据透视表等”的文章,其中详细描述了该过程。 可以在下面看到本文的链接:

Queries, matrices, pivot tables and more

查询,矩阵,数据透视表等

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

We create a local dataset for the Whigs (see above). However there is one difference this time in that we must add the @yearr parameter (discussed immediately above) as the value of year must be passed to the sub report’s stored procedure.

我们为辉格党创建一个本地数据集(见上文)。 但是,这一次有一个区别,因为必须将@yearr参数(在上面直接讨论)添加参数,因为year的值必须传递到子报表的存储过程中。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

As a reminder, the code behind the dataset may be seen above.

提醒一下,数据集背后的代码可以在上方看到。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

We create a parameter called @yearr in a similar fashion to the parameters that we have created in past chats (see above).

我们以类似于过去聊天中创建的参数的方式创建了一个称为@yearr的参数(请参见上文)。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

Finally, we set the Parameters of the dataset “WhigDS” to point to our “@yearr” parameter and the value to be passed to the stored procedure. We click “OK” to leave the “Dataset properties” dialogue box. We find ourselves upon our design surface.

最后,我们将数据集“ WhigDS”的参数设置为指向我们的“ @yearr”参数,并将其值传递给存储过程。 我们单击“确定”离开“数据集属性”对话框。 我们发现自己在设计表面上。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

Our next task is to add a Matrix to our design surface (see above).

我们的下一个任务是在设计图面上添加一个矩阵(请参见上文)。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

We set the dataset property of the Tablix to the dataset (“WhigDS”) that we just created.

我们将Tablix的数据集属性设置为刚刚创建的数据集(“ WhigDS”)。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

As we have done in past get togethers, we remove the Column Groups. Note that we opt to delete only the grouping(see above).

正如我们过去所做的那样,我们删除了列组。 请注意,我们选择仅删除分组(请参见上文)。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

Now that the Tablix’s / Matrix‘s dataset property is pointing to the dataset that we just created, we are able to populate the columns of the matrix (see above).

现在Tablix的/ Matrix的dataset属性指向我们刚创建的数据集,我们可以填充矩阵的列了(见上文)。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

By adding a default year e.g.2013 (PURELY FOR TESTING PURPOSES), we are able to extract the following data for 2013.

通过添加默认年份( 例如 2013)( 纯粹用于测试目的 ),我们可以提取2013年的以下数据。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

By adding a similar default value to the “Tories” report, we are able to obtain the necessary data for the default year, 2013 (see above).

通过向“ Tories”报告中添加类似的默认值,我们可以获得默认年份2013的必要数据(请参见上文)。

We have now finished creating the necessary sub report infrastructure and we are now in a position to connect all the “dots”.

我们现在已经完成了创建必要的子报表基础结构的工作,现在可以连接所有“点”了。

把我们的故事放在一起!! (Putting our story together!!)

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

Back in our main report, we right click upon any of the vertical columns within our chart. The context menu is brought up. We select “Series Properties” (see above).

回到主报告中,右键单击图表中的任何垂直列。 弹出上下文菜单。 我们选择“系列属性”(见上文)。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

The “Series Properties” dialogue box opens. We select the “Action” tab and click upon the “Go to report” radio button (see above).

“系列属性”对话框打开。 我们选择“操作”标签,然后单击“转到报告”单选按钮(请参见上文)。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

Our next task is to “Specify a report”. For this, we shall create an expression (see the expression above). We click “OK” to leave the “Expressions” dialogue box.

我们的下一个任务是“指定报告”。 为此,我们将创建一个表达式(请参见上面的表达式)。 我们单击“确定”离开“表达式”对话框。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

Our last task is to add the @yearr parameter (to run the report) along with the value for the year “[yearr]” (which will automatically come from the “Categories Group”). The two (the parameter and its argument) are then passed to the respective stored procedure.

我们的最后一个任务是添加@yearr参数(以运行报告)以及年份“ [yearr]”的值(该值将自动来自“类别组”)。 然后,将两(参数和它的参数)被传递到相应的存储过程。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

让我们给我们的主要报告打个招呼吧!! (Let us give our Main report a whirl!!)

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

Once again, we click the “Preview” tab on the main report. We see the results of “Whig” and “Tory” sales for the yearly period 2012 through 2015.

再次,我们单击主报告上的“预览”选项卡。 我们看到2012年至2015年期间的“辉格”和“托里”销售结果。

Now, if we click upon the black colored “Whigs” portion of the column for the year 2013 we find that our sub report displays “Whig” data (see below).

现在,如果我们单击2013年度列的黑色“ Whigs”部分,则会发现子报表显示“ Whig”数据(请参见下文)。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

and if we click upon the turquoise portion of the same vertical column, we find that “Tory” based data is shown.

如果单击同一垂直列的青绿色部分,则会发现显示了基于“ Tory”的数据。

如何在SQL Server Reporting Services中使用表达式来创建有效的报告

Thus our main report and our two sub reports have been successfully created and connected. We have therefore achieved what we started out to achieve for this “get-together”.

因此,我们的主报告和两个子报告已成功创建并连接。 因此,我们已经实现了我们为“聚会”而开始实现的目标。

One final note. It has often been suggested to have only the one matrix and to filter the data (to be shown within the matrix). This will work if and only if the structure of both sub reports is the same. In our case at hand, they are, however, the method that we have just been discussing permits flexibility and the ability to select from a plethora of heterogeneous reports.

最后一点。 通常建议仅使用一个矩阵并过滤数据(在矩阵内显示)。 当且仅当两个子报表的结构相同时,这才起作用。 但是,就我们当前的情况而言,它们就是我们刚刚讨论的方法,它具有灵活性以及从众多异构报告中进行选择的能力。

结论 (Conclusions)

Versatility is of prime importance when it comes to creating efficient and effective reports. Expressions utilized in a wise fashion can make the difference between a “hum drum” report and one that is truly dynamic at run time. I trust that you will attempt this exercise with some of your reports and as always, your feedback is always appreciated.

在创建高效的报告时,多功能性至关重要。 以明智的方式使用的表达可以使“嗡嗡声”报告与运行时真正动态的报告有所不同。 我相信您会尝试通过一些报告来进行此练习,并且一如既往,您的反馈意见将始终受到赞赏。

Until our next “fire side” chat, happy programming!

直到我们的下一个“火方”聊天,编程愉快!

参考资料 (References)

翻译自: https://www.sqlshack.com/use-expressions-within-sql-server-reporting-services-create-efficient-reports/