I have this query in Postgres:
SELECT DISTINCT P.nome, P.cognome, F.nomeFacolta, F.id, D.orelez
FROM Persona P, Docenza D, InsErogato IE, Facolta F
WHERE D.id_inserogato = IE.id
AND IE.id_facolta = F.id
AND D.id_persona = P.id
AND D.orelez = ANY ( SELECT MAX(D2.orelez)
FROM Facolta F2, Docenza D2, Inserogato IE2
WHERE D2.id_inserogato = IE2.id
AND IE2.id_facolta = F2.id
AND IE2.annoaccademico = '2009/2010'
GROUP BY F2.id)
ORDER BY D.orelez DESC;
With this result:
nome | cognome | NomeFacolta | id | orelez |
Francesco Bortolan Medicina 7 128000 <---
Giampaolo Dalle Vedove Economia 2 98000 <---
Mauro Spera Scienze 1 87000 <---
Alessandra Salomoni Economia 2 80000
Alessandro Natucci Economia 2 80000
Attilio Boner Medicina 7 80000
Mario Rosario Buffelli Lettere 8 76000 <---
Carlo Capelli Lettere 8 72000
etc ...
I want to change or integrate my query to take only the MAX orelez
value for id
(I indicated with an arrow the tuples that I want to keep in the result). Also I want my query takes only the first three attributes, discarding the last two.
This is the desired output for above example:
nome | cognome | NomeFacolta
Francesco Bortolan Medicina
Giampaolo Dalle Vedove Economia
Mauro Spera Scienze
Mario Rosario Buffelli Lettere
etc ...
How can I change or integrate my query to do this?