RANK Function  OVER (PARTITION BY

 

,RANK() over (partition by   to_char(CI.DAT_REALIZACAO,’mm/yyyy’)                                      –,b.mes_ano_ref
ORDER by  CI.DAT_REALIZACAO) RANK

Irá mostrar a ordenação pelo ranking, exemplo:

11/04/2015 – 1

12/04/2015 – 2

13/04/2015 – 3

13/04/2015 – 3

15/04/2015 – 4

16/04/2015 – 5

16/04/2015 – 5

Hoje me deparei com o erro; ORA-01476 divisor is equal to zero, em um relatório, onde existia o cálculo de divisão.

O problema: Divisão por 0 (zero)

0/0

 

Solução:

CASE

select
case WHEN 0 (var) = 0 then 0
else
50 / 1
end soma
from dual
Resultado: 0

select
case WHEN 1 (var) = 0 then 0
else
50 / 2
end soma
from dual

Resultado: 25

Fonte: http://dba-oracle.com/t_ora_01476_divisor_equa_to_zero.htm

http://stackoverflow.com/questions/15798910/oracle-divisor-is-equal-to-zero

(2014/10)

Olá hoje vou demonstrar como utilizar o gerador de dados do PL/SQL, uma funcionalidade bastante útil quando se precisa popular uma tabela para testes.

 

Criando a tabela

— Create table
create table TB_PESSOA_FISICA
(
nr_sequencia NUMBER,
nm_pessoa VARCHAR2(200),
dt_nascimento DATE,
sexo CHAR(1),
ds_rg VARCHAR2(40),
nr_cpf NUMBER(11),
ds_passaporte VARCHAR2(100),
nr_celular NUMBER,
ds_email VARCHAR2(100),
ds_site VARCHAR2(100)
)
tablespace DEV_DATA
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 5M
next 5M
minextents 1
maxextents unlimited
pctincrease 0
);
— Add comments to the columns
comment on column TB_PESSOA_FISICA.sexo
is ”’M”,”F”’;

 

Após abrir o PL/SQL Developer abrir a funcionalidade no menu ferramentas -> Data generator

gerador

 

A geração pode ser feita de 3 formas, sendo para teste visual, popular diretamente a tabela ou gerar um arquivo .sql, conforme exemplo abaixo.

Resultado em arquivo .sql

insert into DEV.TB_PESSOA_FISICA (NR_SEQUENCIA, NM_PESSOA, SEXO, DS_RG, DS_PASSAPORTE, NR_CELULAR, DS_EMAIL, DS_SITE)
values (676682, ‘Katie Basinger’, ‘?’, ‘702874840’, ‘BR-063921453’, 887188870, ‘biazrvr@zqhwdvj.com’, ‘www.uweovptrlr.com’);

insert into DEV.TB_PESSOA_FISICA (NR_SEQUENCIA, NM_PESSOA, SEXO, DS_RG, DS_PASSAPORTE, NR_CELULAR, DS_EMAIL, DS_SITE)
values (275765, ‘Matthew Emmerich’, ‘?’, ‘437142418’, ‘BR-259837828’, 068887587, ‘yriogou@ouspdgb.com’, ‘www.lmcjkxhgrq.com’);

insert into DEV.TB_PESSOA_FISICA (NR_SEQUENCIA, NM_PESSOA, SEXO, DS_RG, DS_PASSAPORTE, NR_CELULAR, DS_EMAIL, DS_SITE)
values (444395, ‘Leelee Lewis’, ‘?’, ‘712642762’, ‘BR-297344731’, 844415207, ‘dkbggnc@rfnwnyp.com’, ‘www.blngqslczo.com’);

insert into DEV.TB_PESSOA_FISICA (NR_SEQUENCIA, NM_PESSOA, SEXO, DS_RG, DS_PASSAPORTE, NR_CELULAR, DS_EMAIL, DS_SITE)
values (337775, ‘Sonny Bright’, ‘?’, ‘046284636’, ‘BR-389647948’, 551661358, ‘wuutgkw@tnoaivb.com’, ‘www.asztavilcd.com’);

insert into DEV.TB_PESSOA_FISICA (NR_SEQUENCIA, NM_PESSOA, SEXO, DS_RG, DS_PASSAPORTE, NR_CELULAR, DS_EMAIL, DS_SITE)
values (493276, ‘Gaby Tomlin’, ‘?’, ‘386886248’, ‘BR-599360178’, 076930526, ‘mdrgkbt@izlmwsz.com’, ‘www.blesssyuot.com’);

insert into DEV.TB_PESSOA_FISICA (NR_SEQUENCIA, NM_PESSOA, SEXO, DS_RG, DS_PASSAPORTE, NR_CELULAR, DS_EMAIL, DS_SITE)
values (586205, ‘Lila Furtado’, ‘?’, ‘665230936’, ‘BR-655403956’, 247439149, ‘ixmpmuz@pkuyfsc.com’, ‘www.rjfjnkgwuz.com’);

 

SELECT
NM_PESSOA, DT_NASCIMENTO, DS_EMAIL
FROM
PESSOA_FISICA_TB P
WHERE P.SEXO = ‘M’
AND (

(case when 2 = :PARAMETRO then P.TIPO end) in (1)
or
(case when 2 = :PARAMETRO then P.TIPO end) <> (1)

)

 

 

AND ((case when ‘S’ = :P_FERR then
1 end) = 1
or
(case when ‘N’ = :P_FERR then CE.NUM_CONTRATO end) <> (123)
)

PL/SQL: exists / not-exists (Subqueries with EXISTS or NOT EXISTS)

create table FABIANO_CIDADE
(
NR_SEQUENCIA NUMBER,
NM_CIDADE    VARCHAR2(110)
)

select_fabiano_cidade

— Create table
create table FABIANO_PESSOA
(
CD_PESSOA number,
NM_PESSOA varchar2(200),
NR_CIDADE number
)
;

select_fabiano_pessoa

 

select * from
tasy.fabiano_cidade c
where exists (select 1 from
tasy.fabiano_pessoa p
where p.nr_cidade = c.nr_sequencia);

 

plsql_exists

select * from
tasy.fabiano_cidade c
where not exists (select 1 from
tasy.fabiano_pessoa p
where p.nr_cidade = c.nr_sequencia);

 

plsql_not_exists

 

http://docs.oracle.com/cd/E17952_01/refman-5.1-en/exists-and-not-exists-subqueries.html