PostgreSQL unnest() with element number

后端 未结 5 1924
情话喂你
情话喂你 2020-11-21 15:21

When I have a column with separated values, I can use the unnest() function:

myTable
id | elements
---+------------
1  |ab,cd,efg,hi
2  |jk,lm,n         


        
5条回答
  •  悲哀的现实
    2020-11-21 16:15

    If the order of element is not important, you can

    select 
      id, elem, row_number() over (partition by id) as nr
    from (
      select
          id,
          unnest(string_to_array(elements, ',')) AS elem
      from myTable
    ) a
    

提交回复
热议问题