LIMIT 子句

指定要从整个结果集或从分区结果集的窗口返回的结果集行数最大值。

语法

应用于整个结果集:

   LIMIT { num-rows | ALL }

应用于分区结果集的窗口:

   LIMIT num‑rows OVER ( PARTITION BY {{< codevar >}}column‑expr‑x{{< /codevar >}}, ORDER BY {{< codevar >}}column‑expr‑y{{< /codevar >}} [ASC | DESC]

参数

num‑rows
要返回的行的最大数量。
ALL
返回所有行,仅当 LIMIT 应用于整个结果集时才有效。
OVER()
指定如何就当前行而言对输入数据进行分区和排序。输入数据是查询在对 FROM、WHERE、GROUP BY 和 HAVING 子句求值之后返回的结果集。

有关详细信息,请参阅下面的将 LIMIT 与窗口分区结合使用

限制返回的行

LIMIT 指定只返回已查询数据集中的前 k 行。行优先级通过查询的 ORDER BY 子句确定。

例如,以下查询将返回表 customer_dimension 中的前 10 行数据,按列 store_regionnumber_of_employees 的顺序排列:

=> SELECT store_region, store_city||', '||store_state location, store_name, number_of_employees
     FROM store.store_dimension WHERE number_of_employees <= 12 ORDER BY store_region, number_of_employees LIMIT 10;
 store_region |    location    | store_name | number_of_employees
--------------+----------------+------------+---------------------
 East         | Stamford, CT   | Store219   |                  12
 East         | New Haven, CT  | Store66    |                  12
 East         | New York, NY   | Store122   |                  12
 MidWest      | South Bend, IN | Store134   |                  10
 MidWest      | Evansville, IN | Store30    |                  11
 MidWest      | Green Bay, WI  | Store27    |                  12
 South        | Mesquite, TX   | Store124   |                  10
 South        | Cape Coral, FL | Store18    |                  11
 South        | Beaumont, TX   | Store226   |                  11
 South        | Houston, TX    | Store33    |                  11
(10 rows)

将 LIMIT 与窗口分区结合使用

您可以使用 LIMIT 对查询结果应用窗口分区,并限制每个窗口中返回的行数:

SELECT ... FROM dataset LIMIT num‑rows OVER ( PARTITION BY column‑expr‑x, ORDER BY column‑expr‑y [ASC | DESC] )

其中查询 dataset 在每个 column‑expr‑x 分区中返回具有最高或最低 column‑expr‑y 值的 num‑rows 个行。

例如,以下语句对表 store.store_dimension 进行了查询并在结果集上指定了窗口分区。LIMIT 设置为 2,所以每个窗口分区最多只能显示两行。OVER 子句指定按 store_region 对结果集进行分区,其中每个分区窗口显示某个区域中员工人数最少的两个商店:

=> SELECT store_region, store_city||', '||store_state location, store_name, number_of_employees FROM store.store_dimension
     LIMIT 2 OVER (PARTITION BY store_region ORDER BY number_of_employees ASC);
 store_region |      location       | store_name | number_of_employees
--------------+---------------------+------------+---------------------
 West         | Norwalk, CA         | Store43    |                  10
 West         | Lancaster, CA       | Store95    |                  11
 East         | Stamford, CT        | Store219   |                  12
 East         | New York, NY        | Store122   |                  12
 SouthWest    | North Las Vegas, NV | Store170   |                  10
 SouthWest    | Phoenix, AZ         | Store228   |                  11
 NorthWest    | Bellevue, WA        | Store200   |                  19
 NorthWest    | Portland, OR        | Store39    |                  22
 MidWest      | South Bend, IN      | Store134   |                  10
 MidWest      | Evansville, IN      | Store30    |                  11
 South        | Mesquite, TX        | Store124   |                  10
 South        | Beaumont, TX        | Store226   |                  11
(12 rows)