Sort in the Dataset

  • Last update:  2024-02-24
  • Overview

    Application Scenario

    You can sort data in datasets through SQL statements during database queries. Although this sort method boosts the best performance, all calculations must be done in the database, which can result in complex SQL statements and troublesome maintenance. Additionally, some calculations cannot be completed through SQL statements. Therefore, you are advised to use SQL statements to perform simple and single sorts (ascending/descending).

    This document introduces sort types (that can be set when you use SQL statements) when you define a dataset.

    ORDER BY Keyword

    The keywords of ORDER BY are used to sort the result set by one or more columns. The sort is performed in ascending order (namely the ASC keyword) by default. If you need to sort a result set in descending order, you can use the DESC keyword.

    For example, the SQL statements for sorts in ascending and descending orders are as follows:

    Ascending order: SELECT * FROM Sales_Volume ORDER BY Sales_Volume

    Descending order: SELECT * FROM Sales_Volume ORDER BY Sales_Volume DESC

    Example

    Ascending Order

    Create a general report, create a database query, and enter the SQL statement SELECT * FROM Sales_Volume ORDER BY Sales_Volume.

    Click the Preview icon. You can view that data is sorted in ascending order based on the Sales_Volume column, as shown in the following figure.

     1.png

    Descending Order

    Create a general report, create a database query, and enter the SQL statement SELECT * FROM Sales_Volume ORDER BY Sales_Volume DESC.

    Click the Preview icon. You can view that data is sorted in descending order based on the Sales_Volume column, as shown in the following figure.

     2.png

    Multi-Column Sort

    Create a general report, create a database query, and enter the SQL statement SELECT * FROM Sales_Volume ORDER BY Region, Sales_Volume.

    Click the Preview icon. You can view that data is first sorted in ascending order by the Region column and then by the Sales_Volume column in ascending order based on the corresponding region, as shown in the following figure.

     3.png

    Dynamic Sort

    Define a dataset parameter and add a dynamic parameter in the title to sort data by clicking the title.

    Data Preparation

    Create a general report, create a database query named ds1, and enter the SQL query statement SELECT * FROM Sales_Volume WHERE Region='North China' ORDER BY Sales_Volume ${a}.

    The defined dataset parameter a is used to represent the ASC/DESC order, as shown in the following figure.

    iconNote:
    If you need to set dynamic parameters for both two fields, you can modify the SQL statement to SELECT * FROM Sales_Volume WHERE Region='North' ORDER BY Sales_Volume ${a}, SalesPerson ${a}.

     4.png

    Report Design

    Drag data from the dataset into the table, set Data Setting of cell A2 to List, and set the report style, as shown in the following figure.

     5.png

    Dynamic Parameter Adding

    Select cell E1 where the column header Sales Volume is located, add a dynamic parameter (the parameter a set in the SQL statement) in Hyperlink, and enter the formula if(a="desc","asc","desc") in Value.

    iconNote:
    The value of the parameter a is asc by default.

     6.png

    Effect Display

    On PC

    Save the template and click Pagination Preview. The preview effect is shown in the following figure.

     7.gif

    On Mobile Terminals

    The template can be previewed on both the DataAnalyst and HTML5 apps. The effect is shown in the following figure.

     8.gif

    Template Download

    You can download the example template Dynamic Sort by Clicking the Column Header.cpt.

    Notes

    Invalid Sort Based on the First Letter of Chinese Pinyin in MySQL

    If the MySQL database cannot sort data by the first letter of Chinese Pinyin, you need to check whether the UTF-8 encoding is used in the table field. If so, you need to use the CONVERT () function in MySQL to convert the UTF-8 encoding into the GBK encoding and then sort data. Additionally, the GBK character set must have been installed in MySQL, otherwise an error will occur.

    The SQL statement is as follows:

    Ascending order: SELECT * FROM Sales_Volume ORDER BY CONVERT (Product USING gbk)

    Descending order: SELECT * FROM Sales_Volume ORDER BY CONVERT (Product USING gbk) DESC


    Attachment List


    Theme: Report Application
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    10s後關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy