Re: Funcion ventana nueva consulta

From: Anthony <asotolongo(at)uci(dot)cu>
To: Conrado Blasetti <conrado(at)mapfre(dot)com(dot)ar>
Cc: "pgsql-es-ayuda(at)postgresql(dot)org" <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: Re: Funcion ventana nueva consulta
Date: 2012-05-24 15:33:00
Message-ID: 4FBE54AC.1010301@uci.cu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

El 24/05/2012 11:05, Conrado Blasetti escribió:
>
> Anthony, gracias por responder.
>
> Justamente, cuando aplicamos el where, el sistema me devuelve solo el
> período seleccionado y no acumulando lo de períodos anteriores. Lo que
> necesitaría es (si es posible) resolver en el mismo sql la agrupación
> anterior, el detalle y la agrupación posterior
>
> *De:* Anthony [mailto:asotolongo(at)uci(dot)cu]
> *Enviado el:* jueves, 24 de mayo de 2012 12:03
> *Para:* Conrado Blasetti
> *CC:* pgsql-es-ayuda(at)postgresql(dot)org
> *Asunto:* Re: [pgsql-es-ayuda] Funcion ventana nueva consulta
>
> El 24/05/2012 9:45, Conrado Blasetti escribió:
>
> Julio, muchas gracias por responder.
>
> Ahora, se me plantea una nueva duda con respecto a la sentencia
> (perdón por no haberla expuesto en el msg anterior)
>
> Teniendo los datos de esta manera (se agrega una fecha)
>
> drop table foo;
>
> create table foo (
>
> id serial not null,
>
> tipo numeric(10) not null,
>
> fecha date not null,
>
> imp numeric(10,2) not null,
>
> CONSTRAINT foo_id PRIMARY KEY (id)
>
> );
>
> insert into foo (fecha,tipo, imp) values ('01/01/2012'::date,1,30.5);
>
> insert into foo (fecha,tipo, imp) values ('02/01/2012'::date,1,20.25);
>
> insert into foo (fecha,tipo, imp) values ('03/01/2012'::date,2,30);
>
> insert into foo (fecha,tipo, imp) values ('04/01/2012'::date,1,75.3);
>
> insert into foo (fecha,tipo, imp) values ('05/01/2012'::date,1,75.3);
>
> insert into foo (fecha,tipo, imp) values ('06/01/2012'::date,2,100);
>
> insert into foo (fecha,tipo, imp) values ('15/04/2012'::date,1,30.5+7);
>
> insert into foo (fecha,tipo, imp) values ('16/04/2012'::date,1,20.25+7);
>
> insert into foo (fecha,tipo, imp) values ('17/04/2012'::date,2,30+7);
>
> insert into foo (fecha,tipo, imp) values ('18/04/2012'::date,1,75.3+7);
>
> insert into foo (fecha,tipo, imp) values ('19/04/2012'::date,1,75.3+7);
>
> insert into foo (fecha,tipo, imp) values ('20/04/2012'::date,2,100+7);
>
> insert into foo (fecha,tipo, imp) values ('22/05/2012'::date,1,30.5+17);
>
> insert into foo (fecha,tipo, imp) values ('22/05/2012'::date,1,20.25+17);
>
> insert into foo (fecha,tipo, imp) values ('23/05/2012'::date,2,30+17);
>
> insert into foo (fecha,tipo, imp) values ('23/05/2012'::date,1,75.3+17);
>
> insert into foo (fecha,tipo, imp) values ('24/05/2012'::date,1,75.3+17);
>
> insert into foo (fecha,tipo, imp) values ('24/05/2012'::date,2,100+17);
>
> SQL agregando la fecha.
>
> select fecha,tipo1, tipo2, sum(imp) over (order by id)
>
> from (select id, fecha, imp as tipo1, 0 as tipo2, imp
>
> from foo
>
> where tipo= 1
>
> union
>
> select id, fecha, 0 as tipo1, imp as tipo2, imp*-1
>
> from foo
>
> where tipo= 2) Aux
>
> La consulta es, cómo hacer para obtener datos "semi" detallados?
>
> Digamos, quiero ver el detalle del mes 4, debería ejecutar el sql con
> el where correspondiente.
>
> Pero el tema es que, la columna SUM, me acumula sobre los registros
> del where (es lógico porque es lo que le pido), estaría necesitando un
> acumulado de imp1 imp2 y sum() para lo anterior a abril, que se
> muestre el detalle de abril movimiento por movimiento (pero que el
> acumulado sume lo anterior) y luego otro acumulado de todo lo que hay
> después de abril de forma resumida, en este caso, todo ordenado por
> fecha, mostrando algo asi:
>
> fecha
>
>
>
> imp1
>
>
>
> imp2
>
>
>
> acumulado
>
>
>
> tipo
>
> null
>
>
>
> 201,35
>
>
>
> 130
>
>
>
> 71,35
>
>
>
> RESUMEN
>
>
>
>
>
>
>
> 15/04/2012
>
>
>
> 37,5
>
>
>
>
> 108,85
>
>
>
> detalle
>
> 16/04/2012
>
>
>
> 27,25
>
>
>
>
> 136,1
>
>
>
> detalle
>
> 17/04/2012
>
>
>
>
> 37
>
>
>
> 99,1
>
>
>
> detalle
>
> 18/04/2012
>
>
>
> 82,3
>
>
>
>
> 181,4
>
>
>
> detalle
>
> 19/04/2012
>
>
>
> 82,3
>
>
>
>
> 263,7
>
>
>
> detalle
>
> 20/04/2012
>
>
>
>
> 107
>
>
>
> 156,7
>
>
>
> detalle
>
>
>
>
>
>
>
> null
>
>
>
> 269,35
>
>
>
> 164
>
>
>
> 262,05
>
>
>
> RESUMEN
>
>
>
>
>
>
> Se podrá resolver también?
>
> Nuevamente, gracias
>
> Saludos,
>
> Conrado
>
>
>
> <http://www.uci.cu/>
>
> bueno agrégale esto al final y ya
> where fecha between '2012-04-01' and '2012-04-30'
>
> ;-)
> saludos
>
> <http://www.uci.cu/>
>
>
>
> <http://www.uci.cu/>
bueno tendras que hacer algo asi como:
(select null::date as fecha,tipo1, tipo2, sum(imp) over (order by id),
'Resumen'::text

from (select id, fecha, imp as tipo1, 0 as tipo2, imp

from foo

where tipo= 1

union

select id, fecha, 0 as tipo1, imp as tipo2, imp*-1

from foo

where tipo= 2) as Aux

where fecha <= '2012-04-01' order by fecha desc limit 1)
union all

(select fecha,tipo1, tipo2, sum(imp) over (order by id), 'Detalle'::text

from (select id, fecha, imp as tipo1, 0 as tipo2, imp

from foo

where tipo= 1

union

select id, fecha, 0 as tipo1, imp as tipo2, imp*-1

from foo

where tipo= 2) Aux

where fecha between '2012-04-01' and '2012-04-30')
union all

(select null::date as fecha,tipo1, tipo2, sum(imp) over (order by
id), 'Resumen'::text

from (select id, fecha, imp as tipo1, 0 as tipo2, imp

from foo

where tipo= 1

union

select id, fecha, 0 as tipo1, imp as tipo2, imp*-1

from foo

where tipo= 2) as Aux

where fecha >= '2012-04-30' order by fecha desc limit 1)

-- order by fecha desc

10mo. ANIVERSARIO DE LA CREACION DE LA UNIVERSIDAD DE LAS CIENCIAS INFORMATICAS...
CONECTADOS AL FUTURO, CONECTADOS A LA REVOLUCION

http://www.uci.cu
http://www.facebook.com/universidad.uci
http://www.flickr.com/photos/universidad_uci

In response to

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Conrado Blasetti 2012-05-24 15:38:03 RE: Funcion ventana nueva consulta
Previous Message Conrado Blasetti 2012-05-24 15:05:24 RE: Funcion ventana nueva consulta