RE: maximo o valor 1 en función postgres

From: Lazaro Ruben Garcia Martinez <lgarciam(at)vnz(dot)uci(dot)cu>
To: "jvenegasperu (dot)" <jvenegasperu(at)gmail(dot)com>, Ayuda <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: RE: maximo o valor 1 en función postgres
Date: 2013-10-29 15:09:25
Message-ID: 294D3D02D5E18D42827B2ECFEADEB688BA00F4BED5@mx-interno.vnz.uci.cu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

El problema es que como no existe el valor que estas buscando, la consulta devuelve NULL entonces maxi = 1 * maxi, seria null = 1 * null, seguiría siendo NULL, lo que debes hacer en este caso es un COALESCE de esta forma:

select COALESCE(max(num_mz), 1) INTO maxi from cat_manzana mz where mz.sector like $1;

Así garantizas que cuando sea NULL, maxi sea 1.

Saludos a todos.

________________________________________
De: pgsql-es-ayuda-owner(at)postgresql(dot)org [pgsql-es-ayuda-owner(at)postgresql(dot)org] En nombre de jvenegasperu . [jvenegasperu(at)gmail(dot)com]
Enviado el: martes, 29 de octubre de 2013 8:49
Para: Ayuda
Asunto: [pgsql-es-ayuda] maximo o valor 1 en función postgres

Buenos dias

requiero obtener el valor 1 en caso que el maximo no se encuentre

aqui las instrucciones con el problema:

/*********************************************************************************************/

for rec in select * from tmp_cargas where id = $2 loop

select max(num_mz) INTO maxi from cat_manzana mz where mz.sector like $1;
-- la instruccion de la linea anterior me devuelve una celda vacia visto con el pgadmin en windows
-- cuando ya existen valores no hay problema obtengo el maximo y se ejecuta el resto normal
-- pero cuando no hay ningun valor me devuelve una celda vacia tendria que empezar de 1.

-- intente resolverlo con esta linea igual no toma ningun valor
maxi = 1 * maxi;

num_currenti = maxi+1;
clvmznai = $1||trim(to_char(num_currenti,'000'));
numero_mzi = trim(to_char(num_currenti,'000'));
insert into cat_manzana (the_geom,clvmzna,sector,georef,numero_mz,num_mz,id_operacion,usuario) values (rec.the_geom,clvmznai,$1,'si',numero_mzi,num_currenti,$3,$4);

end loop;

/*********************************************************************************************/
¿como podria asignarle el valor 1 a la variable maxi en caso de no encontrar el maximo?

--
José Mercedes Venegas Acevedo
cel: Mov. 949808846

mails: jvenegasperu(at)php(dot)net<mailto:jvenegasperu(at)php(dot)net>
jvenegasperu(at)gmail(dot)com<mailto:jvenegasperu(at)gmail(dot)com>

PHP Spanish Docs translator member.
http://www.php.net/manual/es/index.php

________________________________
Fin a la injusticia, LIBERTAD AHORA A NUESTROS CINCO COMPATRIOTAS QUE SE ENCUENTRAN INJUSTAMENTE EN PRISIONES DE LOS EEUU!
http://www.antiterroristas.cu
http://justiciaparaloscinco.wordpress.com

Fin a la injusticia, LIBERTAD AHORA A NUESTROS CINCO COMPATRIOTAS QUE SE ENCUENTRAN INJUSTAMENTE EN PRISIONES DE LOS EEUU!
http://www.antiterroristas.cu
http://justiciaparaloscinco.wordpress.com

-
Enviado a la lista de correo pgsql-es-ayuda (pgsql-es-ayuda(at)postgresql(dot)org)
Para cambiar tu suscripción:
http://www.postgresql.org/mailpref/pgsql-es-ayuda

In response to

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Jaime Casanova 2013-10-29 15:10:46 Re: Indices en tablas
Previous Message Pedro PG 2013-10-29 15:01:17 Indices en tablas