SORT BY 子句

适用于:check marked yes Databricks SQL check marked yes Databricks Runtime

返回按用户指定顺序在每个分区内排序的结果行。 如果有多个分区,SORT BY 可能返回部分有序的结果。 这与 ORDER BY 子句不同,后者可保证输出的总序。

语法

SORT BY { expression [ sort_direction nulls_sort_oder ] } [, ...]

sort_direction
 [ ASC | DEC ]

nulls_sort_order
 [ NULLS FIRST | NULLS LAST ]

参数

  • expression

    任意类型的表达式,用于建立结果返回时所采用的分区本地顺序。

    如果该表达式为文本 INT 值,则会将其解释为 select 列表中的某个列位置。

  • sort_direction

    指定 sort by 表达式的排序顺序。

    • ASC:此表达式的排序方向为升序。
    • DESC:此表达式的排序顺序为降序。

    如果未显式指定排序方向,则默认按升序对行排序。

  • nulls_sort_order

    可选择指定是在非 NULL 值之前还是之后返回 NULL 值。 如果未指定 null_sort_order,则在排序顺序为 ASC 时,Null 排在前面;排序顺序为 DESC 时,Null 排在后面。

    • NULLS FIRST:首先返回 NULL 值,不考虑排序顺序。
    • NULLS LAST:最后返回 NULL 值,不考虑排序顺序。

在指定多个表达式时,从左向右进行排序。 分区中的所有行按第一个表达式排序。 如果第一个表达式有重复的值,则使用第二个表达式来解析重复项组中的顺序,依此类推。 如果所有 order by 表达式中都有重复值,则生成的顺序不确定。

示例

> CREATE TEMP VIEW person (zip_code, name, age)
    AS VALUES (94588, 'Zen Hui', 50),
              (94588, 'Dan Li', 18),
              (94588, 'Anil K', 27),
              (94588, 'John V', NULL),
              (94511, 'David K', 42),
              (94511, 'Aryan B.', 18),
              (94511, 'Lalit B.', NULL);

-- Use `REPARTITION` hint to partition the data by `zip_code` to
-- examine the `SORT BY` behavior. This is used in rest of the
-- examples.

-- Sort rows by `name` within each partition in ascending manner
> SELECT /*+ REPARTITION(zip_code) */ name, age, zip_code FROM person
    SORT BY name;
   Anil K   27    94588
   Dan Li   18    94588
   John V NULL    94588
  Zen Hui   50    94588
 Aryan B.   18    94511
  David K   42    94511
 Lalit B. NULL    94511

-- Sort rows within each partition using column position.
> SELECT /*+ REPARTITION(zip_code) */ name, age, zip_code FROM person
    SORT BY 1;
   Anil K   27    94588
   Dan Li   18    94588
   John V null    94588
  Zen Hui   50    94588
 Aryan B.   18    94511
  David K   42    94511
 Lalit B. null    94511

-- Sort rows within partition in ascending manner keeping null values to be last.
> SELECT /*+ REPARTITION(zip_code) */ age, name, zip_code FROM person
    SORT BY age NULLS LAST;
   18   Dan Li    94588
   27   Anil K    94588
   50  Zen Hui    94588
 NULL   John V    94588
   18 Aryan B.    94511
   42  David K    94511
 NULL Lalit B.    94511

-- Sort rows by age within each partition in descending manner, which defaults to NULL LAST.
> SELECT /*+ REPARTITION(zip_code) */ age, name, zip_code FROM person
    SORT BY age DESC;
   50  Zen Hui    94588
   27   Anil K    94588
   18   Dan Li    94588
 NULL   John V    94588
   42  David K    94511
   18 Aryan B.    94511
 NULL Lalit B.    94511

-- Sort rows by age within each partition in descending manner keeping null values to be first.
> SELECT /*+ REPARTITION(zip_code) */ age, name, zip_code FROM person
    SORT BY age DESC NULLS FIRST;
 NULL   John V    94588
   50  Zen Hui    94588
   27   Anil K    94588
   18   Dan Li    94588
 NULL Lalit B.    94511
   42  David K    94511
   18 Aryan B.    94511

-- Sort rows within each partition based on more than one column with each column having
-- different sort direction.
> SELECT /*+ REPARTITION(zip_code) */ name, age, zip_code FROM person
    SORT BY name ASC, age DESC;
   Anil K   27    94588
   Dan Li   18    94588
   John V null    94588
  Zen Hui   50    94588
 Aryan B.   18    94511
  David K   42    94511
 Lalit B. null    94511