All Products
Search
Document Center

Simple Log Service:ORDER BY clause

Last Updated:Jun 03, 2026

Sorts query and analysis results by specified columns.

Syntax

ORDER BY column_name [DESC | ASC]
Note
  • To sort by multiple columns: ORDER BY column_name1 [DESC | ASC], column_name2 [DESC | ASC].

  • If you omit DESC or ASC, results sort in ascending order by default.

  • If a column contains identical values, the sort order may vary between executions. To ensure stable sorting, specify multiple columns.

Parameters

Parameter

Description

column name

Column to sort by. Can be a log field or aggregate function alias.

DESC

Descending order.

ASC

Ascending order.

Examples

  • Example 1: Count requests by HTTP status code, sorted by count descending.

    • Query and analysis statement

      * |
      SELECT
        count(*) AS PV,
        status
      GROUP BY
        status
      ORDER BY
        PV DESC
    • Results sorted by PV in descending order: 200 (PV: 163135), 206 (PV: 1224), 207 (PV: 1186), 305 (PV: 1185), 301 (PV: 1184), 307 (PV: 1182), 302 (PV: 1180), and 203 (PV: 1177).

  • Example 2: Calculate average latency by Logstore, sorted descending.

    • Query and analysis statement

      * |
      SELECT
        avg(latency) AS avg_latency,
        LogStore
      GROUP BY
        LogStore
      ORDER BY
        avg_latency DESC
    • Results: avg_latency for Logstore test is 3833.0, for Logstore website_log is 2691.13, and for Logstore date is 2608.06.

  • Example 3: Count requests by request duration, sorted ascending.

    Here, content, time, and request_time are JSON log fields.

    Important

    JSON log queries require field path syntax (Query and analyze JSON logs).

    • Prefix field names with the parent JSON path. Example: content.time.request_time.

    • In analysis statements, enclose JSON field names in double quotation marks (""). Example: "content.time.request_time".

    • Query and analysis statement

      * |
      SELECT
        "content.time.request_time",
        count(*) AS count
      GROUP BY
        "content.time.request_time"
      ORDER BY
        "content.time.request_time"
    • Results by content.time.request_time value: 10.0 (count: 145), 11.0 (count: 123), 12.0 (count: 113).