Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

185
Vistas
Select the max value on a multiple table join

I am trying to get the maximum date out of multiple tables when those table have a particular geometry. My tables more or less look like that (of course they're all different but I shortened them in order to make it clearer ):

A table type :

Id, Info, Geometry, Date

And finally I have an other table that looks like that (shortened again) :

B table:

Id, Geometry

Now, what I want to do is to join all my A type tables on Geometry where they intersect with the B table Geometry, and to get the A table that has the most recent date.

I currently have the following request which is working:

UPDATE last_updateT SET date_last_update= S.dateMax 
FROM
    (SELECT B.gid, MAX(A.last_date) AS dateMax
    FROM B
    JOIN A ON ST_Intersects(B.geometry, A.geometry)
    GROUP BY B.gid) S
WHERE T.id = S.gid;

Now I'd like to be able to do that kind of join on multiple table that looks like table A. I've heard of the function GREATEST but I am not sure about how to use it. Also, I use Postgresql if that makes any differences.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

It seems you are looking for UNION ALL, so you can treat the data from different tables as if it were data from only one table:

SELECT 
  b.gid, 
  MAX(x.last_date) AS dateMax
FROM b
JOIN 
(
  SELECT geometry, last_date FROM a
  UNION ALL
  SELECT geometry, last_date FROM aa
  UNION ALL
  SELECT geometry, last_date FROM aaa
) x ON ST_Intersects(b.geometry, x.geometry)
GROUP BY b.gid;
over 4 years ago · Santiago Trujillo Denunciar

0

In broad strokes, MAX is an aggregate function, so you use MAX to get the highest value from the same column over a number of different rows.

GREATEST is a scalar function, you use GREATEST to get the highest value from different columns in the same row.

eg:

SELECT GREATEST(col1,col2,col3)

Greatest: https://www.postgresql.org/docs/9.5/static/functions-conditional.html

Max: https://www.postgresql.org/docs/9.5/static/functions-aggregate.html

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda