I am currently working on a query with PL / SQL which I have not been able to do, the idea of this query is to be able to traverse (or compare) the rows of a table given the upper and lower limits of the table, that is, for example: there is a table called "employees", where the daily salary of a person (which may vary according to the day) is stored (among others), that is, in this table we will have a user id for a date (20180701) and your daily pay, the idea is that the company administrator enter the id of the employee and for the period of time you want to review (that is, a start date and end date) for that period the query should be able to tell you how many times the difference in salary between every day exceeded a threshold for example of 5%
Then the code in question:
(It is important to keep in mind that for the moment the code has been limited to comparing several days and showing only the number of times where the difference is greater than 5% or not)
DECLARE
u NUMBER :=0;
t NUMBER :=0;
r NUMBER :=0;
k NUMBER:= 0;
FOR i IN 0... 5 LOOP
u:= 0;
k:= k+2;
BEGIN
(SELECT * FROM empleados WHERE FECHA = to_number(to_char(To_Date('20180809', 'yyyymmdd') - u, 'yyyymmdd')))a,
(SELECT * FROM empleados WHERE FECHA = to_number(to_char(To_Date('20180809', 'yyyymmdd') - k, 'yyyymmdd')))b,
IF (Trunc (((b.salario - a.salario)/b.salario)*100, 2) < 5%)
t:=t+1;
END IF;
ELSE IF (Trunc (((b.salario - a.salario)/b.salario)*100, 2) > 5%)
r:=r+1:
END IF;
u AS Veces_que_supero_5%,
t AS Veces_que_no_supero_5%
END LOOP
/
When executing the Script the following error is obtained:
ORA-06550: línea 6, columna 1:
PLS-00103: Encountered the symbol "FOR" when expecting one of the following:
begin function pragma procedure subtype type <an identifier>
<a double-quoted delimited-identifier> current cursor delete
exists prior
The symbol "begin" was substituted for "FOR" to continue.
ORA-06550: línea 6, columna 11:
PLS-00103: Encountered the symbol "." when expecting one of the following:
* & - + / at mod remainder rem .. <an exponent (**)> ||
multiset
The symbol ".. was in
DECLARE
u NUMBER :=0;
t NUMBER :=0;
r NUMBER :=0;
k NUMBER:= 0;
FOR i IN 0... 5 LOOP
u:= 0;
k:= k+2;
BEGIN
(SELECT * FROM empleados WHERE FECHA = to_number(to_char(To_Date('20180809', 'yyyymmdd') - u, 'yyyymmdd')))a,
(SELECT * FROM empleados WHERE FECHA = to_number(to_char(To_Date('20180809', 'yyyymmdd') - k, 'yyyymmdd')))b,
IF (Trunc (((b.salario - a.salario)/b.salario)*100, 2) < 5%)
t:=t+1;
END IF;
ELSE IF (Trunc (((b.salario - a.salario)/b.salario)*100, 2) > 5%)
r:=r+1:
END IF;
u AS Veces_que_supero_5%,
t AS Veces_que_no_supero_5%
END LOOP
Excuse me but I do not understand why you mention the declare comparing it with the for loop. On the other hand, change the begin as you suggested and the problem persists with the error
ORA-06550: línea 8, columna 11:
PLS-00103: Encountered the symbol "." when expecting one of the following:
* & - + / at mod remainder rem .. <an exponent (**)> ||
multiset
The symbol ".. was inserted before "." to continue.
ORA-06550: línea 12, columna 2:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:
( - + case mod new not null <an identifier>
<a double-quoted delimited-identifier> <a bind variable>
continue avg count current exists max min prior sql stdde
DECLARE
u NUMBER :=0;
t NUMBER :=0;
r NUMBER :=0;
k NUMBER:= 0;
BEGIN
FOR i IN 0... 5 LOOP
u:= 0;
k:= k+2;
(SELECT * FROM empleados WHERE FECHA = to_number(to_char(To_Date('20180809', 'yyyymmdd') - u, 'yyyymmdd')))a,
(SELECT * FROM empleados WHERE FECHA = to_number(to_char(To_Date('20180809', 'yyyymmdd') - k, 'yyyymmdd')))b,
IF (Trunc (((b.salario - a.salario)/b.salario)*100, 2) < 5%)
t:=t+1;
END IF;
ELSE IF (Trunc (((b.salario - a.salario)/b.salario)*100, 2) > 5%)
r:=r+1:
END IF;
u AS Veces_que_supero_5%,
t AS Veces_que_no_supero_5%
END LOOP