PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Come dividere l'array in righe in Postgresql

Suggerirei di aggiornare la tua versione di Postgres. Tutte le versioni supportate supportano unnest() :

SELECT x.*
FROM (SELECT id, UNNEST(selected_placements) as selected_placement
      FROM  app_data.content_cards
     ) x
WHERE selected_placement IS NOT NULL;

Nelle versioni precedenti, puoi sforzarti di selezionarli uno alla volta. Quanto segue è testato e funziona, anche se nella versione 9.5:

with content_cards as (
     select 1 as id, array['a', 'b', 'c'] as selected_placements
    )
SELECT id, selected_placements[num] as selected_placement
FROM (SELECT cc.*, generate_series(1, ccup.maxup) as num
      FROM content_cards cc CROSS JOIN
           (SELECT MAX(ARRAY_UPPER(cc.selected_placements, 1)) as maxup
            FROM content_cards cc
           ) ccup
     ) x
WHERE selected_placements[num]  IS NOT NULL;