From: | Oswaldo Hernández <listas(at)soft-com(dot)es> |
---|---|
To: | pgsql-es-ayuda(at)postgresql(dot)org |
Subject: | Re: SQL: 3 ultimas filas de cada referencia |
Date: | 2011-01-05 16:53:45 |
Message-ID: | 4D24A219.8010003@soft-com.es |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-es-ayuda |
El 05/01/2011 16:27, Guillermo Villanueva escribió:
> mmm se me ocurre algo así...
> sirve? probalo
>
> select
> (select count(*)
> from historico b
> where b.ref = a.ref and b.oper>=a.oper) cantidad,
> oper,
> ref,
> fecha
> from
> historico a
> where cantidad between 1 and 3
> order by ref, oper desc
>
>
Parece que funciona, quedaría así:
select
operacion, referencia, fecha
from
historico h
where
( select count(*)
from historico h2
where h2.producto = h.producto and h2.fecha > =h.fecha
) < 4
order by
referencia, fecha desc
Esta es otra solución parecida, en la que estaba trabajando, y que
también funciona:
select
operacion, referencia, fecha
from
historico h
where
operacion in (
select h2.id
from historico h2
where h2.producto = h.producto
order by fecha desc
limit 3)
order by
referencia, fecha desc
Gracias a todos los que os habéis interesado en este problema, aunque me
parece que lo ideal seria actualizar y utilizar window functions.
Saludos,
--
Oswaldo Hernández
From | Date | Subject | |
---|---|---|---|
Next Message | Carlos Bazán | 2011-01-05 17:11:13 | Re: Hosting PG en México |
Previous Message | Cesar Erices | 2011-01-05 16:33:27 | Re: [pgsql-es-ayuda] Hosting PG en México |