共用方式為


QUALIFY 子句

適用於: 核取記號為「是」 Databricks SQL 核取記號為「是」 Databricks Runtime 10.4 LTS 和更新版本。

篩選視窗函式的結果。 若要使用 QUALIFY,至少需要有一個視窗函式出現在清單或 子句中SELECTQUALIFY

語法

QUALIFY boolean_expression

參數

  • boolean_expression

    任何評估為結果類型的 boolean表達式。 兩個或多個表達式可以使用邏輯運算元 ( ANDOR) 結合在一起。

    子句中指定的 QUALIFY 表達式不能包含聚合函數。

範例

CREATE TABLE dealer (id INT, city STRING, car_model STRING, quantity INT);
INSERT INTO dealer VALUES
    (100, 'Fremont', 'Honda Civic', 10),
    (100, 'Fremont', 'Honda Accord', 15),
    (100, 'Fremont', 'Honda CRV', 7),
    (200, 'Dublin', 'Honda Civic', 20),
    (200, 'Dublin', 'Honda Accord', 10),
    (200, 'Dublin', 'Honda CRV', 3),
    (300, 'San Jose', 'Honda Civic', 5),
    (300, 'San Jose', 'Honda Accord', 8);

-- QUALIFY with window functions in the SELECT list.
> SELECT
    city,
    car_model,
    RANK() OVER (PARTITION BY car_model ORDER BY quantity) AS rank
  FROM dealer
  QUALIFY rank = 1;
 city     car_model    rank
 -------- ------------ ----
 San Jose Honda Accord 1
 Dublin   Honda CRV    1
 San Jose Honda Civic  1

-- QUALIFY with window functions in the QUALIFY clause.
SELECT city, car_model
FROM dealer
QUALIFY RANK() OVER (PARTITION BY car_model ORDER BY quantity) = 1;
 city     car_model
 -------- ------------
 San Jose Honda Accord
 Dublin   Honda CRV
 San Jose Honda Civic