Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

325
Views
Función para extraer elementos de matriz a diferentes columnas postgresql

Estoy tratando de diseñar una función para resolver este problema. Tengo una columna con ciudades que se ve así.

 1 |Curaçao-Amsterdam 2 |St. Christopher-Essequibo 3 |Texel-Riohacha-Buenos Aires-La Rochelle`

Y he usado esta consulta para extraerla a una matriz de elementos.

 select t2.rut1,t2.rutacompleta, t2.id from ( select regexp_split_to_array(t.rutacompleta, E'[\-]+') as rut1, t.rutacompleta,t.id from ( select id, strpos(ruta, '-') as posinic, strpos(ruta, '-') as posfin, ruta as rutacompleta from dyncoopnet.todosnavios2 ) t ) t2

Eso da este resultado:

 {Curaçao,Amsterdam} {"St. Christopher",Essequibo} {Texel,Riohacha,"Buenos Aires","La Rochelle"}`

Y quiero crear una función para extraer * elementos de matriz a diferentes columnas. He pensado en una función while como esta:

 create or replace function extractpuertos() returns text as $body$ declare i integer; puerto text; begin i := 1 while (i >=1) loop with tv as( select t2.rut1,t2.rutacompleta, t2.id from( select regexp_split_to_array(t.rutacompleta, E'[\-]+') as rut1, t.rutacompleta,t.id from( select id, strpos(ruta, '-') as posinic, strpos(ruta, '-') as posfin,ruta as rutacompleta from dyncoopnet.todosnavios2) t)t2 ) select tv.rut1[i] as puerto from tv; end loop; return puerto; end;

Pero no estoy seguro de que sea una solución adecuada y cómo implementarla. ¿Alguna pista? ¡Gracias por adelantado!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

es lo que tratas de hacer?

crear mesa:

 t=# create table so65 (i int, t text); CREATE TABLE Time: 55.234 ms

rellenar datos:

 t=# copy so65 from stdin delimiter '|'; Enter data to be copied followed by a newline. End with a backslash and a period on a line by itself. >> 1 |Curaçao-Amsterdam 2 |St. Christopher-Essequibo 3 |Texel-Riohacha-Buenos Aires-La Rochelle>> >> >> \. COPY 3 Time: 2856.465 ms

separar:

 t=# select string_to_array(t,'-') from so65; string_to_array ----------------------------------------------- {Curaçao,Amsterdam} {"St. Christopher",Essequibo} {Texel,Riohacha,"Buenos Aires","La Rochelle"} (3 rows) Time: 4.428 ms

a una columna:

 t=# select unnest(string_to_array(t,'-')) from so65; unnest ----------------- Curaçao Amsterdam St. Christopher Essequibo Texel Riohacha Buenos Aires La Rochelle (8 rows) Time: 1.662 ms
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!