Hola Lista Al ejecutar la siguiente consulta sin agrupar por el campo fechaactivacion uno esperaria el siguiente error:
ERROR: column "a.fechaactivacion" must appear in the GROUP BY clause or be used in an aggregate function Pero oh sorpresa, me lleve cuando el motor la ejecuto de forma satisfactoria. La verifique tanto en PostgreSQL 9.5 (PostgreSQL 9.5.4) como en PostgreSQL 9.6 (PostgreSQL 9.6.1) y en ambos funciona igual select a.id,a.identificacion,*fechaactivacion*,min(b.fecha) as minima from usuario as a join adherencia as b on a.id=b.usuario_id *group by 1,2* Explain analyze: HashAggregate (cost=3472.35..3478.14 rows=1447 width=34) (actual time=144.138..144.415 rows=1359 loops=1) Output: a.id, a.identificacion, *a.fechaactivacion*, min(b.fecha) Group Key: *a.id <http://a.id>, a.identificacion* Buffers: shared hit=76 read=1788 -> Hash Join (cost=314.30..3238.78 rows=155712 width=34) (actual time=10.179..91.949 rows=155886 loops=1) Output: a.id, a.identificacion, a.fechaactivacion, b.fecha Hash Cond: (b.usuario_id = a.id) Buffers: shared hit=76 read=1788 -> Seq Scan on adherencia b (cost=0.00..2184.85 rows=155712 width=16) (actual time=0.490..30.102 rows=155886 loops=1) Output: b.id, b.fecha, b.inicia, b.termina, b.tiempoadherido, b.pausaactual_id, b.usuario_id Buffers: shared hit=9 read=1553 -> Hash (cost=307.79..307.79 rows=1447 width=26) (actual time=9.453..9.453 rows=1447 loops=1) Output: a.id, a.identificacion, a.fechaactivacion Buckets: 2048 Batches: 1 Memory Usage: 89kB Buffers: shared hit=67 read=235 -> Seq Scan on usuario a (cost=0.00..307.79 rows=1447 width=26) (actual time=0.019..2.465 rows=1447 loops=1) Output: a.id, a.identificacion, a.fechaactivacion Buffers: shared hit=67 read=235 Planning time: 1.593 ms Execution time: 144.532 ms Trate de recrearlo con tablas en memoria (SELECT * from (VALUES())..) pero si genera error a no agrupar por el campo *fechaactivacion*. La estructura básica de las tablas son: CREATE TABLE usuario ( dtype character varying(31) NOT NULL, id bigint NOT NULL, identificacion character varying(255), login character varying(255) NOT NULL, apellidos character varying(255), nombres character varying(255), estado character varying(255), bytepassword oid, email character varying(255), encodedpassword character varying(255), fechacreacion timestamp without time zone, fechamodificacion timestamp without time zone, idusuariocrea bigint, idusuariomodifica bigint, modologueoobligatorio boolean, tipoagente character varying(255), codigoexterno character varying(255), deleted boolean NOT NULL DEFAULT false, fechaactivacion timestamp without time zone, tipocuenta character varying(255), encodedsegundadclave character varying(255), CONSTRAINT usuario_pkey PRIMARY KEY (id), CONSTRAINT usuario_login_key UNIQUE (login), CONSTRAINT usuario_numeroagente_key UNIQUE (numeroagente) ) -------------- CREATE TABLE adherencia ( id bigint NOT NULL, fecha timestamp without time zone NOT NULL, termina timestamp without time zone, tiempoadherido bigint, inicia timestamp without time zone NOT NULL, usuario_id bigint NOT NULL, pausaactual_id bigint, CONSTRAINT adherencia_pkey PRIMARY KEY (id) ) -- Cordialmente, Ing. Hellmuth I. Vargas S. Esp. Telemática y Negocios por Internet