- Categories:
Semi-structured and structured data functions (Higher-order)
FILTER¶
Filters an array based on the logic in a lambda expression.
Syntax¶
Arguments¶
arrayThe array that contains the elements to be filtered. The array can be semi-structured or structured.
lambda_expressionA lambda expression that defines the filter condition on each array element.
The lambda expression must have only one argument specified in the following syntax:
Returns¶
The return type of this function is an array of the same type as the input array. The returned array contains the elements for which the filter condition returns TRUE.
If either argument is NULL, the function returns NULL without reporting an error.
Usage notes¶
When the data type for the lambda argument is explicitly specified, the array element is coerced into the specified type before lambda invocation. For information about coercion, see Data type conversion.
If the filter condition evaluates to NULL, the corresponding array element is filtered out.
Examples¶
The following examples use the FILTER function.
Filter for array elements greater than a value¶
Use the FILTER function to return objects in an array that have a value greater than or equal to 50:
Filter for array elements that are not NULL¶
Use the FILTER function to return array elements that are not NULL:
Filter for array elements in a table that are greater than or equal to a value¶
Assume you have a table named orders with the columns order_id, order_date, and order_detail. The
order_detail column is an array of the line items, their purchase quantity, and subtotal. The table contains
two rows of data. The following SQL statement creates this table and inserts the rows:
Use the FILTER function to return orders with subtotals that are greater than or equal to 1500:
Reference a table column in a lambda expression to filter array elements in table data¶
Create a table with one column of type ARRAY and another column of type INT:
Use the FILTER function to return the values of array element values in each row that are lower
than the value in col2: