Eder Sousa escreveu:
> Boa tarde!!
>
> Preciso criar uma tabela da seguinte maneira:
>
> xxxxvalor 2 100,00 50 % 50%
> xxxxvalor 1 50,00 25 % 75%
> xxxxvalor 1 50,00 25 % 100%
> .......
>
> Alguém tem uma forma de como montar um SQL (função) que resolva este
> tipo de Problema?
Para a versão 8.3 ou anterior, eu faço desse jeito:
- Habilitar custom_variable_classes no postgresql.conf
custom_variable_classes = 'sistema'
- Crie uma função para incrementar valores:
CREATE OR REPLACE FUNCTION getsoma(numeric)
RETURNS numeric AS
$BODY$
declare
_valor alias for $1;
_soma numeric = 0;
begin
if (trim(current_setting('sistema.user_value')) != '') then
_soma := cast (current_setting('sistema.user_value') as
numeric);
end if;
if (_valor > 0) then
PERFORM set_config('sistema.user_value', cast( (_soma +
_valor) as text), true);
_soma := (_soma + _valor);
end if;
return _soma;
end;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;
- SQL:
begin; -- tem que estar dentro de transação
select set_config('sistema.user_value', '-1', true);
select valor, getSoma(valor) as soma from tabela;
commit;
Taí, espero que ajude.
--
[]´s, André Volpato
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral