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:02:45
Message-ID: 4FBE4D95.5040805@uci.cu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

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

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:05:24 RE: Funcion ventana nueva consulta
Previous Message José Gregorio Hernández 2012-05-24 14:57:32 Programar cargos automáticos mensuales