使用sql语句生成报表_SQL Server报表服务:如何使用报表生成器处理常见的最终用户要求

使用sql语句生成报表

In this article, we will discuss the SQL Server Reporting Service Report Builder and look at how to handle common client, end user requests using Report Builder. Report Builder is a very powerful tool to create a report for the SQL Server Reporting Service.

在本文中,我们将讨论SQL Server Reporting Service报表生成器,并研究如何使用报表生成器处理常见的客户端,最终用户请求。 报表生成器是一个非常强大的工具,可以为SQL Server报表服务创建报表。

Advantages of the Report Builder is that it:

报表生成器的优点是:

  • exists as a Stand-alone installation

    作为独立安装存在
  • can use different data from a lot of data sources

    可以使用来自许多数据源的不同数据
  • can easily deploy reports to SQL Server Reporting Service

    可以轻松地将报表部署到SQL Server报表服务
  • supports full capabilities of SQL Server Reporting Service

    支持SQL Server Reporting Service的全部功能
  • provides a productive report-authoring environment

    提供高效的报告编写环境

After that, we will demonstrate how to handle problems that may arise from the client or business.

之后,我们将演示如何处理客户或业务可能产生的问题。

Because when we create a report for clients, they can request very different features. Our scenario is that we have a client who sells the product to different countries, so we are developing a report for this client. We will find a solution for different needs of this client.

因为当我们为客户创建报告时,他们会要求非常不同的功能。 我们的情况是,我们有一个客户将产品销售到不同的国家,因此我们正在为此客户开发一份报告。 我们将为该客户的不同需求找到解决方案。

You can download Report Builder 2017 in this link and you can install it. At the startup, a screen comes up like below. In this screen, we will click blank report and create a new report.

您可以在此链接中下载Report Builder 2017并进行安装。 在启动时,出现如下屏幕。 在此屏幕中,我们将单击空白报告并创建一个新报告。

We will create sample sales data and retrieve data to report.

我们将创建样本销售数据并检索数据以进行报告。

CREATE TABLE SalesCurrency
(ID INT IDENTITY(1,1) PRIMARY KEY,
SalesAmount FLOAT ,
Cur VARCHAR(10),mail VARCHAR(50))
 
INSERT INTO SalesCurrency 
VALUES (20,'USD' ,'testQQtest.com') ,(10,'GBP','sample.sample.cox'),
(60,'EUR','invalidemail@h.cooom'),
('15','JPY','invalid@lll.coz'),('92','EUR','ppl@test.com')

Client Request: Could you show my different currency sales in USD and exchange the real-time currency rates?

客户要求:您能否以美元显示我的其他货币销售额并交换实时货币汇率?

Response: Yes, we can handle this problem in three steps on Reporting Builder!

响应 :是的,我们可以通过Reporting Builder的三个步骤来解决此问题!

What to do?

该怎么办?

  • We will create an XML datasource that retrieves the daily exchange rates

    我们将创建一个XML数据源来检索每日汇率
  • We will create a dataset and connect this dataset to XML data source

    我们将创建一个数据集并将该数据集连接到XML数据源
  • We will use lookup function and exchange all different currency sales to USD

    我们将使用查找功能,并将所有不同的货币销售额兑换为USD

First of all, we will add a datasource to our report.

首先,我们将数据源添加到我们的报告中。

Then we will select XML connection type and setup a connection string which returns exchange rates. This link will return USD exchange rates. You can find a lot of links which returns exchange rates in different currencies.

然后,我们将选择XML连接类型并设置一个返回汇率的连接字符串。 该链接将返回美元汇率。 您可以找到很多以不同货币返回汇率的链接。

We will add a dataset, connect this dataset to XML datasource and click refresh fields.

我们将添加一个数据集,将该数据集连接到XML数据源,然后单击刷新字段。

Now our XML dataset is ready. We will add new datasource and dataset which retrieves sales data from SQL Server database.

现在我们的XML数据集已准备就绪。 我们将添加新的数据源和数据集,以从SQL Server数据库检索销售数据。

We will write our query to query textbox.

我们将把查询写到查询文本框。

We will add the table and then drag and drop “SalesAmount” and “Cur” fields to table.

我们将添加表格,然后将“ SalesAmount”和“ Cur”字段拖放到表格中。

When we run the report, it will look something like this.

当我们运行报告时,它将看起来像这样。

The most important point in this demo is that we will use the Lookup function. The Lookup function helps us to return matching exchangeRate from XML datasource. It means that the Lookup function helps to find matching values from a different dataset.

该演示中最重要的一点是,我们将使用Lookup函数。 Lookup函数帮助我们从XML数据源返回匹配的exchangeRate。 这意味着“查找”功能有助于从其他数据集中查找匹配值。

The Lookup function gets the following parameters:

查找功能获取以下参数:

  • Lookup(source_expresion,destination_experission,result_experssion,dataset)查找(source_expresion,destination_experission,result_experssion,数据集)
  • source_expresion : specify the value that we want to lookup. In our example, this value would be “Currency” column.source_expresion:指定我们要查找的值。 在我们的示例中,该值为“货币”列。
  • destination_experission : specify the value that we have to match from source dataset. In our example, this value would be “TargetCurrency”destination_experission:指定我们必须从源数据集中匹配的值。 在我们的示例中,该值为“ TargetCurrency”
  • result_experssion: specify the value which we want to return. In our example, this value would be “exchangeRate”result_experssion:指定我们要返回的值。 在我们的示例中,该值为“ exchangeRate”
  • dataset : specify the destination dataset name.数据集:指定目标数据集名称。

Now, we will add a new column to the table and write the Lookup function and exchange currency to USD.

现在,我们将在表中添加一个新列,并编写Lookup函数并将货币兑换为USD。

We will write Lookup experession for converting sales amounts to USD.

我们将编写将销售金额转换为USD的Lookup experession。

Fields!SalesAmount.Value*IIF(Fields!Cur.Value=”USD”,1,Lookup(Fields!Cur.Value,Fields!targetCurrency.Value,1/val(Fields!exchangeRate.Value), “XmlCurrencyDataSource”))

Fields!SalesAmount.Value * IIF(Fields!Cur.Value =“ USD”,1,Lookup(Fields!Cur.Value,Fields!targetCurrency.Value,1 / val(Fields!exchangeRate.Value),“ XmlCurrencyDataSource”))

Finally, our report will show converted sales amount.

最后,我们的报告将显示转换后的销售额。

Client Request: Can you validate an email address using regex function?

客户要求:您可以使用正则表达式功能验证电子邮件地址吗?

Response: Yes, we can handle this problem by creating custom code using regex.

回答:是的,我们可以通过使用正则表达式创建自定义代码来解决此问题。

What to do?

该怎么办?

  • We will write custom function code in report builder.

    我们将在报表生成器中编写自定义功能代码。
  • We will use a custom function in the report.

    我们将在报告中使用自定义函数。

Now we will open report parameters and then click code tab. We will add custom function code which will validate e-mail addresses. In Report Builder we can use VB.net code.

现在,我们将打开报告参数,然后单击“代码”选项卡。 我们将添加用于验证电子邮件地址的自定义功能代码。 在报表生成器中,我们可以使用VB.net代码。

Function validateEmail(emailAddress) As Boolean
        Dim email As New System.Text.RegularExpressions.Regex("([\w-+]+(?:\.[\w-+]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7})")
        If email.IsMatch(emailAddress) Then
            Return True
        Else
            Return False
        End If
    End Function

In this step, we will add two columns into the table which are “Mail” and “MailValidate” columns. We will write an expression for “Mailvalidate” column.

在此步骤中,我们将在表中添加两列,即“ Mail”和“ MailValidate”列。 我们将为“ Mailvalidate”列编写一个表达式。

We will set expression for “MailValidate” column. This custom function will use our custom code and check mail validation.

我们将为“ MailValidate”列设置表达式。 此自定义功能将使用我们的自定义代码并检查邮件验证。

And finally, we will run our report.

最后,我们将运行报告。

Client Request: Can you add filter sales currency with multiple parameters and show selected parameters?

客户要求:您可以添加具有多个参数的过滤器销售货币并显示所选参数吗?

Response: Yes, we can handle this problem to add multiple parameters.

响应:是的,我们可以处理此问题以添加多个参数。

How will we solve this?

我们将如何解决呢?

  • We will add a report parameter

    我们将添加一个报告参数
  • We will get report parameter values from XML data source

    我们将从XML数据源获取报告参数值
  • We will filter sales with this parameter

    我们将使用此参数过滤销售

Now we will add report parameter and get parameter values from XML data source

现在,我们将添加报告参数并从XML数据源获取参数值

We will select “Available Values” tab and chose “Get values from a query”. Then, we will select XML dataset and set the “Value Field” and “Label Field”.

我们将选择“可用值”选项卡,然后选择“从查询中获取值”。 然后,我们将选择XML数据集并设置“值字段”和“标签字段”。

The Value field defines a value which filters the dataset

值字段定义用于过滤数据集的值

The Label field defines a text which is shown in parameter description

标签字段定义了一个文本,该文本显示在参数描述中

We will set multiple “Allow multiple values”. This parameter lets us to select multiple values in the parameter. The “Prompt” text box defines a text which is shown in report parameter.

我们将设置多个“允许多个值”。 此参数使我们可以在参数中选择多个值。 “提示”文本框定义了一个文本,该文本显示在报告参数中。

In this step, we will filter the sales dataset. We will open the dataset properties.

在此步骤中,我们将过滤销售数据集。 我们将打开数据集属性。

In “Filters” tabs we will add new filter expression and set report parameter to this expression. Here, we will use “In” operator because we want to select multiple parameters.

在“过滤器”标签中,我们将添加新的过滤器表达式并将报告参数设置为此表达式。 在这里,我们将使用“ In”运算符,因为我们要选择多个参数。

When we run the report, we are able to see report parameter. This parameter will filter the report.

运行报告时,我们可以看到报告参数。 此参数将过滤报告。

If we want to show selected values, we have to use join function. And after that, we will add a text box to report and write a join expression.

如果要显示选定的值,则必须使用联接功能。 然后,我们将添加一个文本框来报告和编写连接表达式。

如何打开Reporting Service报表和部署报表 (How to open Reporting Service reports and deploy reports)

If we want to deploy reports to Reporting Services, we will select “File tab” and select “Save As” and then select “Recent Sites and Servers”. We will include the Reporting Service URL.

如果要将报表部署到Reporting Services,我们将选择“文件”选项卡,然后选择“另存为”,然后选择“最近的站点和服务器”。 我们将包括Reporting Service URL。

And then select the folder and save the report.

然后选择文件夹并保存报告。

Finally, our report is ready to use in Reporting service.

最后,我们的报告可以在Reporting Service中使用。

结论 (Conclusion)

In this article, we focused on SQL Server Reporting Service Report Builder. This light-weight tool helps us to create reports and publish reports to Reporting Service. If companies are using Reporting Service in their reporting and business intelligence solutions, the Report Builder can provide for creating reporting or self-service BI (Self-Service BI approach helps business users to develop their own reports without the need of IT department. With this approach, business users get rid of dependence on IT department.).

在本文中,我们重点介绍了SQL Server Reporting Service报表生成器。 这个轻巧的工具可帮助我们创建报告并将报告发布到Reporting Service。 如果公司在其报告和商业智能解决方案中使用Reporting Service,则报告生成器可以提供创建报告或自助服务BI(自助服务BI方法,可帮助业务用户自行开发报告,而无需IT部门。)方法,业务用户摆脱了对IT部门的依赖。)

参考资料 (References)

翻译自: https://www.sqlshack.com/sql-server-reporting-service-how-to-handle-common-end-user-requirements-with-report-builder/

使用sql语句生成报表

  • 2
    点赞
  • 0
    评论
  • 3
    收藏
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值