Skip to content
Advertisement

SQL filter elements of array

I have a table of employee similar to this:

I want to get the department, name, and age of all employees, something similar to this:

How can I achieve this using SQL query?

Advertisement

Answer

I assume you are using Hive/Spark and the datatype of the column is an array of maps.

Using explode and collect_list and map functions.

Note that this would be not be as performant as a Spark solution or a UDF with which you can access the required keys in an array of maps, without a function like explode.

One more way to do this with Spark SQL functions transform and map_filter (only available starting Spark version 3.0.0).

Another option with Spark versions > 2.4 is using function element_at with transform and selecting the required keys.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement