# actualizar timestamp ultimo_enviado y ultimo_recibido de contacto
select id, actualizacion, ultimo_enviado, ultimo_recibido from contact;

update contact 
    set ultimo_enviado = (select max(timestamp) from message where contact_id = contact.id and direction = 'outgoing'),
    ultimo_recibido = (select max(timestamp) from message where contact_id = contact.id and direction = 'incoming'),
    actualizacion = (select max(timestamp) from message where contact_id = contact.id and direction in ('outgoing', 'incoming'));

