共用方式為


LATERAL VIEW 子句

適用於: 核取記號為「是」Databricks SQL 核取記號為「是」Databricks Runtime

與之類的 EXPLODE產生器函式搭配使用,其會產生包含一或多個數據列的虛擬數據表。 LATERAL VIEW 會將數據列套用至每個原始輸出數據列。

在 Databricks SQL 中,從 Databricks Runtime 12.2 開始,這個子句已被取代。 您應該叫用數據表值產生器函式做為 table_reference

語法

LATERAL VIEW [ OUTER ] generator_function ( expression [, ...] ) [ table_identifier ] AS column_identifier [, ...]

參數

  • OUTER

    如果 OUTER 指定,如果輸入陣列/對應是空的或 Null,則傳回 null。

  • generator_function

    產生器函式 (EXPLODE、INLINE 等)。

  • table_identifier

    的別名 generator_function,這是選擇性的。

  • column_identifier

    列出 的數據行別名,這些別名 generator_function可用於輸出數據列。 數據行標識碼的數目必須符合產生器函式所傳回的數據行數目。

範例

> CREATE TABLE person (id INT, name STRING, age INT, class INT, address STRING);
> INSERT INTO person VALUES
    (100, 'John', 30, 1, 'Street 1'),
    (200, 'Mary', NULL, 1, 'Street 2'),
    (300, 'Mike', 80, 3, 'Street 3'),
    (400, 'Dan', 50, 4, 'Street 4');

> SELECT * FROM person
    LATERAL VIEW EXPLODE(ARRAY(30, 60)) tableName AS c_age
    LATERAL VIEW EXPLODE(ARRAY(40, 80)) AS d_age;
   id    name     age    class     address    c_age    d_age
 ------ ------- ------- -------- ----------- -------- --------
  100    John    30      1        Street 1    30       40
  100    John    30      1        Street 1    30       80
  100    John    30      1        Street 1    60       40
  100    John    30      1        Street 1    60       80
  200    Mary    NULL    1        Street 2    30       40
  200    Mary    NULL    1        Street 2    30       80
  200    Mary    NULL    1        Street 2    60       40
  200    Mary    NULL    1        Street 2    60       80
  300    Mike    80      3        Street 3    30       40
  300    Mike    80      3        Street 3    30       80
  300    Mike    80      3        Street 3    60       40
  300    Mike    80      3        Street 3    60       80
  400    Dan     50      4        Street 4    30       40
  400    Dan     50      4        Street 4    30       80
  400    Dan     50      4        Street 4    60       40
  400    Dan     50      4        Street 4    60       80

> SELECT c_age, COUNT(1) FROM person
     LATERAL VIEW EXPLODE(ARRAY(30, 60)) AS c_age
     LATERAL VIEW EXPLODE(ARRAY(40, 80)) AS d_age
     GROUP BY c_age;
  c_age    count(1)
 -------- -----------
  60       8
  30       8

SELECT * FROM person
    LATERAL VIEW EXPLODE(ARRAY()) tableName AS c_age;
  id    name    age    class    address    c_age
 ----- ------- ------ -------- ---------- --------

> SELECT * FROM person
     LATERAL VIEW OUTER EXPLODE(ARRAY()) tableName AS c_age;
   id    name     age    class     address    c_age
 ------ ------- ------- -------- ----------- --------
  100    John    30      1        Street 1    NULL
  200    Mary    NULL    1        Street 2    NULL
  300    Mike    80      3        Street 3    NULL
  400    Dan     50      4        Street 4    NULL