The group_concat function is used in report development.

Source: Internet
Author: User

The group_concat function is used in report development.

When using report tools such as Jasper or BIRT, some unconventional statistics are often encountered, and the report tools or SQL statements are difficult to process, such as in MSSQL/Oracle, A report similar to the calculation result of group_concat function is displayed.

The centralized computing appliance has a structured and strong computing engine, which is easy to integrate and can help report tools to easily meet such requirements. The following uses MSSQL as an example to illustrate the implementation process of the general group_concat function.

Table 1 of MSSQL has four fields: Col1, Col2, and Col3 are grouping fields, and Col4 are summary fields. Some data is as follows:


The data sources required by the report tool are as follows:

Code of the Set calculator:

A1 = mssqlDB. query ("select * fromtable1 where Col1 in" + arg)

This code executes the SQL statement to retrieve data from the database. Here, arg is a parameter from the report, for example ). The calculation result of A1 is as follows:

A2 = A1.group (Col1, Col2, Col3 ;~. (Col4). string @ d (): Col4)

This code groups A1 by Col1, Col2, and Col3, and concatenates Col4 in each group of data with commas. ~ In the code ~ Indicates each group of data. For example, the first group has three records .~. (Col4) indicates to retrieve the Col4 fields in each group of data. For example, the first group is the set [A12G3, K78DE, MAT12]. The string function Concatenates the members in the set into a string. The default Delimiter is Comma. Option @ d indicates that no quotation marks are given to the Members. Therefore, the expression [A12G3, K78DE, MAT12] is used. string @ d () is equal to "A12G3, K78DE, MAT12 ". The expression ": Col4" indicates renaming the previous calculation result to Col4.

A3 is the final calculation result of this case, as follows:

A3: result A2

This code returns the data in A2 to the report tool.

The assembler provides a JDBC interface. The report tool recognizes the assembler as a common database. For the integration solution, see related documents.

 

The following uses JasperReport as an example to design a report. The table is as follows:

After previewing, you can see the report results:

Note that the method of the report call set calculator is the same as that of the call stored procedure. For example, save this script as group_concat.dfx, you can use group_concat $ P {arg} in the SQL designer of JasperReport.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.