All Products
Search
Document Center

MaxCompute:TRANS_ARRAY

Last Updated:Jul 12, 2023

Transposes one row of data into multiple rows. This function is a user-defined table-valued function (UDTF) that transposes an array separated by fixed delimiters in a column into multiple rows.

Limits

  • All columns that are used as keys must be placed before the columns that are to be transposed.

  • Only one UDTF is allowed in a SELECT statement.

  • This function cannot be used with the GROUP BY, CLUSTER BY, DISTRIBUTE BY, or SORT BY clause.

Syntax

trans_array (<num_keys>, <separator>, <key1>,<key2>,...,<col1>,<col2>,<col3>) as (<key1>,<key2>,...,<col1>, <col2>)

Parameters

  • num_keys: required. The value is a constant of the BIGINT type and must be greater than or equal to 0. This parameter specifies the number of columns that can be used as keys when you transpose one row into multiple rows.

  • separator: required. The value is a constant of the STRING type. This parameter is used to split a string into multiple elements. If this parameter is left empty, an error is returned.

  • keys: required. This parameter specifies the columns that are used as keys when you transpose one row into multiple rows. The number of keys is specified by num_keys. If all columns are used as keys and num_keys is equal to the total number of all columns, only one row is returned.

  • cols: required. This parameter specifies the array that you want to transpose into rows. All columns that follow keys are considered arrays to be transposed. The value of this parameter must be of the STRING type to store arrays in the STRING format, such as Hangzhou;Beijing;shanghai. The values in this array are separated by semicolons (;).

Return value

Transposed rows are returned. The new column name is specified by as. The data types of columns that are used as keys remain unchanged. All other columns are of the STRING type. The number of transposed rows is based on the array with the maximum number of elements. If the number of rows is insufficient, the value null is added.

Examples

  • Example 1: The t_table table contains the following data:

    +----------+----------+------------+
    | login_id | login_ip | login_time |
    +----------+----------+------------+
    | wangwangA | 192.168.0.1,192.168.0.2 | 20120101010000,20120102010000 |
    | wangwangB | 192.168.45.10,192.168.67.22,192.168.6.3 | 20120111010000,20120112010000,20120223080000 |
    +----------+----------+------------+
    -- Execute the following SQL statement: 
    select trans_array(1, ",", login_id, login_ip, login_time) as (login_id,login_ip,login_time) from t_table;
    -- The following result is returned: 
    +----------+----------+------------+
    | login_id | login_ip | login_time |
    +----------+----------+------------+
    | wangwangB | 192.168.45.10 | 20120111010000 |
    | wangwangB | 192.168.67.22 | 20120112010000 |
    | wangwangB | 192.168.6.3 | 20120223080000 |
    | wangwangA | 192.168.0.1 | 20120101010000 |
    | wangwangA | 192.168.0.2 | 20120102010000 |
    +----------+----------+------------+
    
    -- The table contains the following data: 
    Login_id LOGIN_IP LOGIN_TIME 
    wangwangA 192.168.0.1,192.168.0.2 20120101010000
    -- The value null is added to supplement the array in which data is insufficient.  
    Login_id Login_ip Login_time 
    wangwangA 192.168.0.1 20120101010000
    wangwangA 192.168.0.2 NULL
  • Example 2: The mf_fun_array_test_t table contains the following data:

    +------------+------------+------------+------------+
    | id         | name       | login_ip   | login_time |
    +------------+------------+------------+------------+
    | 1          | Tom        | 192.168.100.1,192.168.100.2 | 20211101010101,20211101010102 |
    | 2          | Jerry      | 192.168.100.3,192.168.100.4 | 20211101010103,20211101010104 |
    +------------+------------+------------+------------+
    
    -- Use the id and name columns as keys to transpose data in the table. Execute the following SQL statement: 
    select trans_array(2, ",", Id,Name, login_ip, login_time) as (Id,Name,login_ip,login_time) from mf_fun_array_test_t;
    -- The following result is returned: 
    +------------+------------+------------+------------+
    | id         | name       | login_ip   | login_time |
    +------------+------------+------------+------------+
    | 1          | Tom        | 192.168.100.1 | 20211101010101 |
    | 1          | Tom        | 192.168.100.2 | 20211101010102 |
    | 2          | Jerry      | 192.168.100.3 | 20211101010103 |
    | 2          | Jerry      | 192.168.100.4 | 20211101010104 |
    +------------+------------+------------+------------+

Related functions

For more information, see Other functions.