happysundar
6/27/2014 - 5:58 PM

filtering out all non-null elements from an array in postgresql

filtering out all non-null elements from an array in postgresql

WITH T1 AS (
    SELECT
      program_id,
      ARRAY [alias_title,
      alias_title_2,
      alias_title_3,
      alias_title_4] AS title_array
    FROM movies
),
    T2 AS (
      SELECT
        program_id,
        unnest(title_array) AS alias_title
      FROM T1 )
SELECT
  program_id,
  array_agg(alias_title)
FROM t2
WHERE alias_title IS NOT NULL
GROUP BY program_id
LIMIT 50;