Hola Alvaro

Gracias por la respuesta; si tiene razon: agrupe solo por id y siguió
funcionado... :-)


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



HashAggregate  (cost=2217.64..2220.74 rows=1035 width=34) (actual
time=240.780..241.232 rows=941 loops=1)
  Output: a.id, a.identificacion, a.fechaactivacion, min(b.fecha)
  Group Key: a.id
  Buffers: shared hit=91 read=1204
  ->  Hash Join  (cost=92.73..2099.46 rows=118182 width=34) (actual
time=1.659..175.634 rows=121101 loops=1)
        Output: a.id, a.identificacion, a.fechaactivacion, b.fecha
        Hash Cond: (b.usuario_id = a.id)
        Buffers: shared hit=91 read=1204
        ->  Seq Scan on adherencia b  (cost=0.00..1563.55 rows=118182
width=16) (actual time=0.039..58.533 rows=121101 loops=1)
              Output: b.id, b.fecha, b.termina, b.tiempoadherido, b.inicia,
b.usuario_id, b.pausaactual_id
              Buffers: shared hit=5 read=1204
        ->  Hash  (cost=89.11..89.11 rows=1035 width=26) (actual
time=1.608..1.608 rows=1035 loops=1)
              Output: a.id, a.identificacion, a.fechaactivacion
              Buckets: 2048  Batches: 1  Memory Usage: 69kB
              Buffers: shared hit=86
              ->  Seq Scan on usuario a  (cost=0.00..89.11 rows=1035
width=26) (actual time=0.010..1.021 rows=1035 loops=1)
                    Output: a.id, a.identificacion, a.fechaactivacion
                    Buffers: shared hit=86
Planning time: 0.220 ms
Execution time: 241.635 ms




El 16 de febrero de 2017, 11:46, Alvaro Herrera<alvhe...@2ndquadrant.com>
escribió:

> Hellmuth Vargas escribió:
> > 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.
>
> Funciona porque el sistema sabe que "id" es llave primaria de la tabla
> a, por lo tanto fechaactivación (de la misma tabla) tiene necesariamente
> que ser un único valor por grupo.
>
> > Trate de recrearlo con tablas en memoria (SELECT * from (VALUES())..)
> pero
> > si genera error a no agrupar por el campo *fechaactivacion*.
>
> Acá no funciona porque no hay llave primaria que permita hacer la
> deducción.
>
> --
> Álvaro Herrera                https://www.2ndQuadrant.com/
> PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
>



-- 
Cordialmente,

Ing. Hellmuth I. Vargas S.
Esp. Telemática y Negocios por Internet
Oracle Database 10g Administrator Certified Associate
EnterpriseDB Certified PostgreSQL 9.3 Associate

Responder a