博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL Server中的报表–使用SQL Server数据工具将三个报表合并为一个报表
阅读量:2526 次
发布时间:2019-05-11

本文共 19286 字,大约阅读时间需要 64 分钟。

介绍 (Introduction)

In our last “fireside chat” we discussed a few of the challenges that the HR Manager of a major hardware chain was experiencing. Mary Smith, the HR manager has since approached us to modify her existing reports to function more efficiently and effectively by utilizing her existing data, yet reduce the total number of reports.

在上一次“炉边聊天”中,我们讨论了主要硬件链的HR经理遇到的一些挑战。 此后,人事经理玛丽·史密斯(Mary Smith)向我们求助,以利用其现有数据来修改其现有报告,以更有效地运作,同时减少报告总数。

We shall be looking at “Sales By Employee Gender” report and examine step by step, how three reports may be combined into one single report. Our end version of the “Male Sales” is shown below:

我们将查看“按性别划分的销售额”报告,并逐步检查如何将三份报告合并为一份报告。 我们的“男性销售”最终版本如下所示:

And the female version of the SAME PHYSICAL report is shown below:

下面显示了SAME PHYSICAL报告的女性版本:

Enough said, let us get started!

够了,让我们开始吧!

入门 (Getting Started)

We begin our journey by having a quick look at Mary’s data.

我们通过快速查看Mary的数据开始我们的旅程。

Opening SQL Server Management Studio we open the SQL Shack database and have a look at an extract from the “HardwareSales” table (see below).

打开SQL Server Management Studio,我们打开SQL Shack数据库,并查看“ HardwareSales”表中的摘录(见下文)。

We note that Mary has a separate column for the Males Sales and one for the Female Sales, by Year and Month.

我们注意到,玛丽按年份和月份分别对男性销售和女性销售有单独的一列。

We begin by creating a small function that accepts a “Year Month” combination e.g. 201502 and calculates the first three letters of the month’s name. In this case “Feb”. The code for this function may be seen below in Addenda1.

我们首先创建一个小函数,该函数接受“ Year Month”组合,例如201502,并计算该月名称的前三个字母。 在这种情况下,“ Feb”。 此功能的代码可以在下面的附录1中看到。

Our field containing the Month name “Monthee” (obtained from our function) may be seen above.

我们的字段包含月份名称“ Monthee”(从我们的函数获得)可以在上方看到。

Having obtained the data that we require to construct Mary’s report, we now proceed to create a stored procedure that may be utilized to pull the necessary data for our Reporting Services report. The code for our stored procedure may be seen below:

获得了构建Mary报告所需的数据后,我们现在继续创建一个存储过程,该过程可用于为Reporting Services报告提取必要的数据。 我们的存储过程的代码如下所示:

We note that when the stored procedure is called, that our report will pass a year value to the stored procedure.

我们注意到当调用存储过程时,我们的报告将把年份值传递给存储过程。

Having reached this point, we are now ready to construct our report.

至此,我们现在准备构建报告。

创建我们的报告 (Creating our report)

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

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

Should you have never worked with Reporting Services or should you not feel comfortable creating a Reporting Services project, then please do have a look at one of our earlier chats where the “creation” process is described in detail.

如果您从未使用过Reporting Services,或者您对创建Reporting Services项目感到不自在,请查看我们之前的聊天中详细描述“创建”过程的内容。

Having opened Visual Studio, we create our project and give it the name “HardwareSales1” (see below).

打开Visual Studio之后,我们创建我们的项目并将其命名为“ HardwareSales1”(见下文)。

We find ourselves on our drawing surface (see below).

我们发现自己在图纸表面上(见下文)。

Our first task is to create a “Shared Data Source” which will connect to our relational database table. As we have discussed in past “get togethers”, the database may be likened to the water faucet on the side of your house. The “data source” may then be likened to a water hose that will carry the data to where it is required.

我们的第一个任务是创建一个“共享数据源”,它将连接到我们的关系数据库表。 正如我们过去讨论过的“聚在一起”一样,该数据库可以比喻为房屋侧面的水龙头。 然后,可以将“数据源”比喻为将数据携带到所需位置的水管。

We “right click” on the “Shared Data Sources” tab and select “Add New Data Source” as shown above.

我们在“共享数据源”选项卡上“右键单击”,然后选择“添加新数据源”,如上所示。

The “Shared Data Source” properties dialog box is brought into view (see above). We click “Edit” to create our “Connection string”.

进入“共享数据源”属性对话框(请参见上文)。 我们单击“编辑”以创建“连接字符串”。

The “Connection Properties” dialogue box is brought up (see above).

出现“连接属性”对话框(见上文)。

We simply enter the name of the SQL Server and the name of the database where the data resides (see above).

我们只需输入SQL Server的名称和数据所在的数据库的名称(请参见上文)。

We click “Ok” and “OK” again to completer the process. We find ourselves back upon our drawing surface (see below).

我们再次单击“确定”和“确定”以完成该过程。 我们发现自己回到了图纸表面(见下文)。

We note that the “Shared Data Source” that we just created is now present under the “Shared Data Sources” tab.

我们注意到,我们刚刚创建的“共享数据源”现在位于“共享数据源”选项卡下。

Our next step is to create a Report.

我们的下一步是创建报告。

We “Right Click upon the “Reports” folder (see above) and we select “Add” and then “New Item”.

我们“右键单击”“报告”文件夹(见上文),然后选择“添加”,然后选择“新建项目”。

The “Add New Item” dialogue box appears. We select “Report” and give our report the name “HardwareSales1” (see above). We click “Add” to return to our drawing surface (see below).

出现“添加新项”对话框。 我们选择“报告”,并给我们的报告命名为“ HardwareSales1”(见上文)。 我们单击“添加”以返回到绘图表面(见下文)。

Utilizing our hose and garden example. Having the water faucet (the database) connected to the Shared Data Source (the hose), we wish to be economical with our water and permit the hose to empty the water into a watering can. This “watering can” is our dataset and thus far it does not exist.

以我们的软管和花园为例。 将水龙头(数据库)连接到共享数据源(软管)后,我们希望节约用水,并允许软管将水倒入喷壶中。 此“喷壶”是我们的数据集,到目前为止尚不存在。

Let us create a “local “dataset.

让我们创建一个“本地”数据集。

We right click upon the “Datasets” tab and select “Add Dataset” as may be seen above.

我们右键单击“数据集”选项卡,然后选择“添加数据集”,如上所示。

The “Dataset Properties” dialogue box is brought into view (see above). We select “Use a dataset embedded in my report”. We click the “New” button to create a “link” between this new dataset (that we are creating) and the stored procedure that we just created (see above).

出现“数据集属性”对话框(请参见上文)。 我们选择“使用报表中嵌入的数据集”。 我们单击“新建”按钮以在此新数据集(正在创建)和我们刚刚创建的存储过程之间创建“链接”(请参见上文)。

The “Data Source Properties” dialogue box appears (see above).

出现“数据源属性”对话框(见上文)。

We select the “Use shared data source reference” option (see above). We click “OK” to exit this screen.

我们选择“使用共享数据源引用”选项(请参见上文)。 我们单击“确定”退出此屏幕。

Returning to our “Dataset Properties” screen, we select our stored procedure (create above).

返回“数据集属性”屏幕,我们选择存储过程(在上面创建)。

We click the “Refresh Fields” button which will pull the field names from the stored procedure and validate them (see below).

我们单击“刷新字段”按钮,该按钮将从存储过程中提取字段名称并进行验证(请参见下文)。

The fields appear in the screen dump above. Our last task in this step is to verify that “Reporting Services” has detected that we require a “Year” parameter to be passed to the stored procedure.

这些字段显示在上方的屏幕转储中。 我们在此步骤中的最后一项任务是验证“ Reporting Services”是否已检测到我们需要将“ Year”参数传递给存储过程。

Clicking upon the “Parameters” tab, we note that the parameter “@Year” has been detected. In other words, Reporting Services has parsed the stored procedure and noted that the stored procedure is expecting a value of a year to be passed from the report to the query.

单击“参数”选项卡,我们注意到已检测到参数“ @Year”。 换句话说,Reporting Services已经分析了存储过程,并指出该存储过程期望将一年的值从报表传递到查询。

The astute reader will note that the value of the parameter is blank. Let us tend to this straight away. We click “OK” to leave this dialogue box and we find ourselves back upon our drawing surface.

精明的读者会注意到该参数的值为空白。 让我们立即趋向于此。 我们单击“确定”离开该对话框,然后我们回到绘图表面。

Before creating our “Year” parameter, we must create one final dataset that pulls the unique values for the “years” contained within Mary’s data. The process is the same as we have described above. The final dataset layout may be seen below:

在创建“年份”参数之前,我们必须创建一个最终数据集,以提取玛丽数据中包含的“年份”的唯一值。 该过程与我们上面描述的相同。 最终的数据集布局如下所示:

One final point, it must be remembered that the first four characters of the field “Month” contain the actual year.

最后一点,必须记住,“月”字段的前四个字符包含实际年份。

创建一个“年份”参数 (Creating a “year” parameter)

By “right clicking” on the “Parameters” tab, the “Add Parameter” option appears (see above). We click “Add Parameter”.

通过在“参数”选项卡上单击“右键”,将出现“添加参数”选项(请参见上文)。 我们点击“添加参数”。

The “Report Parameter Properties” dialogue box appears (see above).

出现“报告参数属性”对话框(见上文)。

We give our parameter the name “Year” (see above). We now set our “Available Values” (see below).

我们将参数命名为“年份”(见上文)。 现在,我们设置“可用值”(见下文)。

We inform Reporting Services that we shall be obtaining our “Year” arguments from the “Year” dataset that we created above. We click “OK” to leave the parameter box.

我们告知Reporting Services,我们将从上面创建的“年份”数据集中获取“年份”参数。 我们单击“确定”离开参数框。

Now that we have our “Year” parameter created, we return briefly to our “DataSet1” dataset (which will contain our sales data), selecting the “Parameters” tab and modify the “Value” box to contain [@Year] (see below).

现在我们已经创建了“ Year”参数,我们将短暂返回到“ DataSet1”数据集(它将包含我们的销售数据),选择“ Parameters”选项卡并修改“ Value”框以包含[@Year] (请参见下面)。

At this point if we run our report and have a quick look at our “Parameter” banner (see below), we note that “2014” and “2015” appear as report options (reflecting the actual years within Mary’s data).

此时,如果我们运行报告并快速浏览“ Parameter”(参数)标语(见下文),我们会注意到“ 2014”和“ 2015”作为报告选项出现(反映了Mary数据内的实际年份)。

We are now in the position where our report “Infrastructure” is complete. Now comes the tricky part.

现在,我们的报告“基础设施”已经完成。 现在是棘手的部分。

Our report which we are creating is gender-based and as such it is obvious that we have two unique values (i.e. M(ale) and F(emale). We create another parameter (Audience) the same manner described above, however, this time considering the nature of the parameter, I have “hard-wired” the values (see below).

我们正在创建的报告基于性别,因此很明显,我们有两个唯一值(即M(ale)和F(emale)),我们以与上述相同的方式创建了另一个参数(受众群体),考虑到参数的性质,我已经将这些值“硬接线”了(见下文)。

A quick re-run of our query will give us an idea of how the parameter selection will appear (see below).

快速重新运行查询可以使我们了解如何选择参数(见下文)。

创建我们的报告图表 (Creating our report chart)

We begin by “dragging” a chart onto our work surface from the Reporting Services toolbox. We select a “Column” chart (see below).

我们首先从Reporting Services工具箱中将图表“拖动”到工作表上。 我们选择一个“柱状图”(见下文)。

We click “OK” to place the chart upon our drawing surface (see below).

我们单击“确定”以将图表放置在我们的绘图表面上(见下文)。

Now that we have our chart on our drawing surface, we must inform the chart where it will obtain its source data and this is from the dataset (Dateset1) which will contain our sales data (see below).

现在我们将图表放在绘图面上,我们必须通知该图表它将在何处获取其源数据,该数据来自包含销售数据的数据集(Dateset1)(请参见下文)。

By “Right Clicking upon our chart, the “Chart Data” dialogue box is displayed (see above). Under normal circumstances “Σ Values” box (see above) would contain either the “Male Sales” data or the “Women Sales” data.

通过“右键单击我们的图表”,将显示“图表数据”对话框(请参见上文)。 在正常情况下,“Σ值”框(见上文)将包含“男性销售”数据或“女性销售”数据。

Now hang on a bit, we could add both fields and be done with it, HOWEVER, we want this “fireside chat” to be a bit more innovative. Let us assume for the sake of argument that based upon which “Audience” the end user chooses, he or she wants that data displayed within one and only one report column! This is what we are going to implement.

现在稍等片刻,我们可以添加两个字段并进行处理,但是,我们希望这种“炉边聊天”更具创新性。 为了争辩,让我们假设最终用户基于选择的“受众群体”,希望将其数据显示在一个且仅一个报告列中! 这就是我们要实现的。

To configure our “Σ Values” box, we click the + and select expression (see above). The “Expression” dialogue box open (see below).

要配置“Σ值”框,请单击+并选择表达式(请参见上文)。 “表达式”对话框打开(请参见下文)。

We enter the following expression into the “Expression” dialogue box.

我们在“表达式”对话框中输入以下表达式。

= (IIf(Parameters!Audience.Value = “M”, Fields!MaleSales.Value

,Fields!FemaleSales.Value))

=(IIf(Parameters!Audience.Value =“ M”,字段!MaleSales.Value

,Fields!FemaleSales.Value))

This done, our next step is to place the date-related fields (Month and Monthee) into the “Category Groups” (see below).

完成此操作后,我们的下一步是将与日期相关的字段(“月”和“月”)放入“类别组”(请参见下文)。

We now right-click on the Category Property of Month and select “Category Group Properties (see below).

现在,我们右键单击“月份”的“类别”属性,然后选择“类别组属性”(请参见下文)。

We open the “Expressions” tab next to the “Label” month and change the value in the expressions box (see below)

我们打开“标签”月份旁边的“表达式”标签,然后在表达式框中更改值(请参见下文)

to

The reason we do this is that we do not want the year and the month to show up on the report. We rather want the actual month name to be reflected. The astute reader will note that the only reason that we utilized the year and month in the first place was to ensure that the month names appeared in the correct order and not from April to September (as would be alphabetical).

我们这样做的原因是我们不希望年份和月份出现在报告中。 我们宁愿反映实际的月份名称。 精明的读者会注意到,我们首先使用年份和月份的唯一原因是要确保月份名称以正确的顺序出现,而不是从4月到9月(按照字母顺序)。

We have one last thing to do before applying the cosmetics to the report.

在将化妆品应用于报表之前,我们还有最后一件事要做。

We must alter the horizontal axis to show every month.

我们必须更改水平轴以显示每个月。

We right-click on the horizontal axis and select “Horizontal Axis Properties”(see above). The “Horizontal Axis Properties” dialog box opens (see below).

我们右键单击水平轴,然后选择“水平轴属性”(见上文)。 “水平轴属性”对话框打开(请参见下文)。

We change the inteval from “Auto” to 1 (see below).

我们将间隔从“自动”更改为1(请参见下文)。

We click “OK” to accept our changes and we are returned to our drawing surface.

单击“确定”接受更改,然后返回到工程图表面。

让我们看看到目前为止 (Let us see what we have thus far)

Clicking the “Preview” tab we set the “Year” to 2014 and we set the Audience to “MaleSales” and click “View Report”. The rendered report may be seen below.

单击“预览”选项卡,将“年份”设置为2014,将受众群体设置为“ MaleSales”,然后单击“查看报告”。 呈现的报告可以在下面看到。

Great!!! Now let us try “Women Sales”.

大!!! 现在,让我们尝试“女性销售”。

We can see that the report retains the same appearance however the financial details have changed. The only issue being that the legend still states “MaleSales”. Let us fix this.

我们可以看到该报告保留了相同的外观,但是财务详细信息已更改。 唯一的问题是,图例中仍然标有“ MaleSales”。 让我们解决这个问题。

We right click on the “Series” properties as may be seen below:

我们右键单击“系列”属性,如下所示:

The “Series Property” dialogue box opens (see below).

“系列属性”对话框打开(请参见下文)。

We click on the function tab to the side of the ”Custom legend text” label (see above).

我们单击“自定义图例文本”标签旁边的功能选项卡(见上文)。

We enter the following expression within the dialog box.

我们在对话框中输入以下表达式。

=IIf(Parameters!Audience.Value=“M”, “MaleSales”,”WomenSales”)

= IIf(参数!Audience.Value = “ M”,“ MaleSales”,“ WomenSales” )

We click “OK” to set the expression and return to our drawing surface.

单击“确定”以设置表达式并返回到绘图表面。

Running our query once again we find that the label is now correct for “MaleSales”

再次运行查询,我们发现标签现在对“ MaleSales”正确

The same is true for “Women Sales”

“女性销售”也是如此

加矩阵 (Adding a matrix)

To improve our report, Mary had asked us to include a matrix so that she could easily see the dollar amounts.

为了改进我们的报告,玛丽要求我们包括一个矩阵,以便她可以轻松看到美元金额。

We begin by dragging a matrix control from the toolbox to the drawing surface (and place it below the chart) see below.

首先,将矩阵控件从工具箱拖动到绘图表面(并将其放置在图表下方),如下所示。

We set the “DataSetName” property to the same value that we utilized for the chart.

我们将“ DataSetName”属性设置为用于图表的相同值。

We set the first column to hold the year and month “Month”.

我们将第一列设置为保留年份和月份“月”。

We note that the “Row Groups” is now set to the “year month” combination and this is exactly what we want.

我们注意到,“行组”现在设置为“年月”组合,这正是我们想要的。

We must remove the grouping on the “Column Groups”. We do so by right-clicking on the Value “Column Group” and selecting “Delete Group” (see below)

我们必须删除“列组”上的分组。 我们通过右键单击“列组”值并选择“删除组”来做到这一点(见下文)

We are now asked if we want to delete just the grouping or the grouping and the data.

现在,询问是否要删除分组仅删除分组和数据。

We select the “Delete group only” radio button (see above).

我们选择“仅删除组”单选按钮(请参见上文)。

Now here is the grizzly part!

现在这里是灰熊的部分!

We would normally select a field such as “MaleSales” or “WomenSales” see below:

我们通常会选择一个字段,例如“ MaleSales”或“ WomenSales”,如下所示:

This would be the wrong answer for this exercise. What we need to do is to right click on the text box (see above) and select “Textbox Properties” (see below).

这将是此练习的错误答案。 我们需要做的是右键单击文本框(请参见上文),然后选择“文本框属性”(请参见下文)。

The “Textbox Properties” dialogue box opens.

“文本框属性”对话框打开。

We open the function box to for the “Value” box and add the same code that we utilized for the chart.

我们打开“值”框的功能框,并添加用于图表的相同代码。

= (IIf(Parameters!Audience.Value = “M”, Fields!MaleSales.Value

,Fields!FemaleSales.Value))

=(IIf(Parameters!Audience.Value =“ M”,字段!MaleSales.Value

,Fields!FemaleSales.Value))

The code (above) having been placed in the Expression dialogue box. We click “OK” to accept.

(上面的)代码已放置在“表达式”对话框中。 我们单击“确定”接受。

Running our report for “MaleSales” we obtain the following (see above) and

运行针对“ MaleSales”的报告,我们获得以下信息(请参见上文),以及

“WomensSales” may be seen above.

上面可以看到“ WomensSales”。

Let us fix the glaring issue. Our column containing the sales has no title.

让我们解决明显的问题。 我们包含销售的列没有标题。

Once again, we open the properties box HOWEVER this time for the column header and for its “Value” we enter the following code within the expression box.

再次,我们这次打开列标题的属性框,但对于其“值”,我们在表达式框中输入以下代码。

This code will enable us to have the correct column header above the financial figures.

此代码将使我们能够在财务数字上方具有正确的列标题。

Running our report once again, we find for “Male Sales”…

再次运行我们的报告,我们发现“男性销售”…

and for “Women Sales”

以及“女性销售”

应用“砰砰声” (Applying the “bangers and whistles”)

While the necessary information is contained in our report, the final task that we have is to tidy the report format. We shall format the vertical axis of the chart to reflect “$” (in addition to $’s within the matrix). We shall place value labels on the bar chart as well (see below).

虽然我们的报告中包含必要的信息,但我们的最后任务是整理报告格式。 我们将格式化图表的垂直轴以反映“ $”(矩阵中除$之外)。 我们还将在条形图上放置值标签(请参见下文)。

Should you be unfamiliar with formatting charts and matrices, do have a look at one of my earlier SQL Shack articles where the complete process is described in detail. Failing that, do feel free to contact me via SQL Shack. I promise I do answer all questions (if I can).

如果您不熟悉图表和矩阵的格式,请阅读我之前SQL Shack文章中的一篇,其中详细描述了完整的过程。 否则,请随时通过SQL Shack与我联系。 我保证我会回答所有问题(如果可以的话)。

So we now come to the end of another “get together”. I hope that this article has given you some food for thought.

因此,我们现在结束另一个“聚会”的结尾。 我希望本文能给您一些思考的东西。

结论 (Conclusions)

Report real estate is often limited and we all attempt to overload our reports, primarily due to end-user requirements. This is often a challenge. This said, with a bit of ingenuity we can have all the user required information yet manage to maintain some semblance of order upon our drawing surface. Whilst what we discussed today is rather simplistic, there are a plethora of other ways of controlling information visibility lying at your fingertips.

报表空间通常有限,我们所有人都主要由于最终用户的要求而试图超载报表。 这通常是一个挑战。 这就是说,只要有一点点巧思,我们就可以拥有所有用户所需的信息,同时又设法在我们的绘图表面上保持一定程度的顺序。 尽管我们今天讨论的内容非常简单,但是还有许多其他控制信息可见性的方法唾手可得。

As always, should you have any questions, please do contact me.

与往常一样,如果您有任何疑问,请与我联系。

In the interim, happy programming!

在此期间,编程愉快!

附录1 (Addenda 1)

   USE [SQLShack]GO SET ANSI_NULLS ONGO SET QUOTED_IDENTIFIER ONGO create function [dbo].[Monthee](@YearMth as varchar(6))    RETURNS Varchar(6)   AS   BEGIN   declare @Return  varchar(6) BeginSet @return = case when right(@YearMth,2) = '01' then 'Jan'     when right(@YearMth,2) = '02' then 'Feb'     when right(@YearMth,2) = '03' then 'Mar'     when right(@YearMth,2) = '04' then 'Apr'     when right(@YearMth,2) = '05' then 'May'     when right(@YearMth,2) = '06' then 'Jun'     when right(@YearMth,2) = '07' then 'Jul'     when right(@YearMth,2) = '08' then 'Aug'     when right(@YearMth,2) = '09' then 'Sep'     when right(@YearMth,2) = '10' then 'Oct'     when right(@YearMth,2) = '11' then 'Nov'     when right(@YearMth,2) = '12' then 'Dec' else 'XXX' end	 end 	 RETURN(@return)endGO 

参考文献: (References:)

翻译自:

转载地址:http://vriwd.baihongyu.com/

你可能感兴趣的文章
Git(四) - 分支管理
查看>>
PHP Curl发送数据
查看>>
HTTP协议
查看>>
CentOS7 重置root密码
查看>>
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Laravel框架学习笔记之任务调度(定时任务)
查看>>
Swagger在Laravel项目中的使用
查看>>
Laravel 的生命周期
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
jxl写入excel实现数据导出功能
查看>>
linux文件目录类命令|--cp指令
查看>>
.net MVC 404错误解决方法
查看>>
linux系统目录结构
查看>>
git
查看>>
btn按钮之间事件相互调用
查看>>
Entity Framework 4.3.1 级联删除
查看>>