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

172
Views
Postgresql: seleccione solo si se completa más de una columna

Estos son mis nombres de columna en una tabla llamada st:

 st4stat st8stat st1stat st11stat st6stat st10stat st2stat st7stat st12stat

Necesito hacer una selección (elegir sus valores) solo cuando más de UNA de estas columnas no son nulas. No puedo decir ninguna combinación posible. ¿Hay alguna manera rápida para esto?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Convierta cada prueba en entero:

 where (col1 is not null)::int + (col2 is not null)::int + (col3 is not null)::int + (col4 is not null)::int > 1
over 4 years ago · Santiago Trujillo Report

0

No conozco ninguna forma agradable de hacerlo sin crear una función (o varias), pero hay un par de formas no tan bonitas.

Podrías hacerlo con un feo

 SELECT * from ROW WHERE (CASE WHEN col2 is null THEN 0 ELSE 1 END + CASE WHEN col2 is null THEN 0 ELSE 1 END + CASE WHEN col3 is null THEN 0 ELSE 1 END + ...) > 1;

También podemos hacerlo un poco mejor con arreglos si las columnas tienen el mismo tipo de datos:

 SELECT * from ROW WHERE array_length(array_remove(ARRAY[col1, col2, col3, col4, ...], NULL), 1) > 1;

Esto crea una matriz de los valores de la columna, luego elimina todos los NULL y, si queda más de un valor en la matriz, se imprime la fila.

Si las columnas tienen diferentes tipos, podemos usar IS NULL para tomar los valores booleanos y hacer lo mismo:

 SELECT * from ROW WHERE array_length(array_remove(ARRAY[col1 IS NULL, col2 IS NULL, col3 IS NULL, col4 IS NULL, ...], true), 1) > 1;
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!