PL/SQL Conditional Statement - oracle

This is regarding PL/SQL conditional statements. Since I'm new to this language, can someone help me to find the error on this code? The error msg is given below.
DECLARE
stock_rec stock%ROWTYPE;
var_company stock.company%TYPE := 'IBM';
BEGIN
SELECT * INTO stock_rec
FROM stock s
WHERE s.company = var_company;
BEGIN
IF (stock_rec.price < 45) THEN
DBMS_PUTPUT.PUT_LINE('Current price is very low !');
ELSIF (stock_rec.price >= 45 AND stock_rec.price < 55) THEN
DBMS_PUTPUT.PUT_LINE('Current price is low !');
ELSIF (stock_rec.price >= 55 AND stock_rec.price < 65) THEN
DBMS_PUTPUT.PUT_LINE('Current price is medium !');
ELSIF (stock_rec.price >= 65 AND stock_rec.price < 75) THEN
DBMS_PUTPUT.PUT_LINE('Current price is medium high !');
ELSE (stock_rec.price >= 75)
DBMS_PUTPUT.PUT_LINE('Current price is high !');
END IF;
END;
END;
/
-------error msg---------------------------------------------
ORA-06550: line 17, column 40:
PLS-00103: Encountered the symbol ")" when expecting one of the following:
& - + / at mod remainder rem <an exponent (**)> and or as
|| multiset
The symbol "* was inserted before ")" to continue.

The last ELSE should actually be ELSIF (and then you have THEN missing):
ELSIF (stock_rec.price >= 75) THEN
Also, you most probably don't need inner BEGIN-END.
DECLARE
stock_rec stock%ROWTYPE;
var_company stock.company%TYPE := 'IBM';
BEGIN
SELECT * INTO stock_rec
FROM stock s
WHERE s.company = var_company;
IF (stock_rec.price < 45) THEN
DBMS_PUTPUT.PUT_LINE('Current price is very low !');
ELSIF (stock_rec.price >= 45 AND stock_rec.price < 55) THEN
DBMS_PUTPUT.PUT_LINE('Current price is low !');
ELSIF (stock_rec.price >= 55 AND stock_rec.price < 65) THEN
DBMS_PUTPUT.PUT_LINE('Current price is medium !');
ELSIF (stock_rec.price >= 65 AND stock_rec.price < 75) THEN
DBMS_PUTPUT.PUT_LINE('Current price is medium high !');
ELSIF (stock_rec.price >= 75) THEN
DBMS_PUTPUT.PUT_LINE('Current price is high !');
END IF;
END;
/

You can simplify the code to use a single DBMS_OUTPUT.PUT_LINE statement if you use a CASE expression:
DECLARE
stock_rec stock%ROWTYPE;
var_company stock.company%TYPE := 'IBM';
BEGIN
SELECT *
INTO stock_rec
FROM stock
WHERE company = var_company;
DBMS_PUTPUT.PUT_LINE(
CASE
WHEN stock_rec.price < 45
THEN 'Current price is very low !'
WHEN stock_rec.price < 55
THEN 'Current price is low !'
WHEN stock_rec.price < 65
THEN 'Current price is medium !'
WHEN stock_rec.price < 75
THEN 'Current price is medium high !'
WHEN stock_rec.price >= 75
THEN 'Current price is high !'
ELSE NULL
END
);
END;
/

Related

pl sql payment calculation

I need help with PVA calculation in PL/SQL .I have formula:
Annuity = r * PVA Ordinary / [1 – (1 + r)-n]
Where:
PVA Ordinary = Present value of an ordinary annuity
r = Effective interest rate
n = Number of periods.
enter image description here
You can use:
DECLARE
principal NUMBER := 9000;
r NUMBER := 0.015;
n NUMBER := 5;
start_dt DATE := DATE '2022-07-14';
payment NUMBER := r * principal / (1 - POWER(1 + r, -n));
amt NUMBER := principal;
interest NUMBER;
pmt_dt DATE;
BEGIN
FOR i IN 1 .. n LOOP
pmt_dt := ADD_MONTHS(start_dt, i);
pmt_dt := pmt_dt + CASE pmt_dt - TRUNC(pmt_dt, 'IW')
WHEN 5 THEN 2 -- Saturday
WHEN 6 THEN 1 -- Sunday
ELSE 0 -- Weekday
END;
interest := amt * r;
amt := amt - payment + interest;
DBMS_OUTPUT.PUT_LINE(
TO_CHAR(i, 'fm0')
|| ', ' || TO_CHAR(pmt_dt, 'YYYY-MM-DD (DY)')
|| ', ' || TO_CHAR(payment, '9990.00')
|| ', ' || TO_CHAR(interest, '990.00')
|| ', ' || TO_CHAR(payment - interest, '9990.00')
|| ', ' || TO_CHAR(amt, '9990.00')
);
END LOOP;
END;
/
Which outputs:
1, 2022-08-15 (MON), 1881.80, 135.00, 1746.80, 7253.20
2, 2022-09-14 (WED), 1881.80, 108.80, 1773.01, 5480.19
3, 2022-10-14 (FRI), 1881.80, 82.20, 1799.60, 3680.59
4, 2022-11-14 (MON), 1881.80, 55.21, 1826.60, 1853.99
5, 2022-12-14 (WED), 1881.80, 27.81, 1853.99, -0.00
Or, in SQL using a MODEL clause:
WITH data (id, start_date, principal, rate, period ) AS (
SELECT 1, DATE '2022-07-14', 9000, 0.015, 5 FROM DUAL
)
SELECT pmt_dt + CASE pmt_dt - TRUNC(pmt_dt, 'IW')
WHEN 5 THEN 2
WHEN 6 THEN 1
ELSE 0
END AS pmt_dt,
ROUND(payment, 2) AS payment,
ROUND(interest, 2) AS interest,
ROUND(payment - interest, 2) AS reduction,
ROUND(balance, 2) AS balance
FROM data
MODEL
PARTITION BY (id)
DIMENSION BY (1 AS key)
MEASURES (
start_date,
principal,
rate,
period,
DATE '1900-01-01' AS pmt_dt,
rate * principal / (1 - POWER(1 + rate, -period)) AS payment,
0 AS interest,
0 AS balance
)
RULES SEQUENTIAL ORDER ITERATE (100) UNTIL (balance[ITERATION_NUMBER+1] <= 0) (
payment[ITERATION_NUMBER+1] = payment[1],
pmt_dt[ITERATION_NUMBER+1] = ADD_MONTHS(start_date[1], ITERATION_NUMBER+1),
interest[ITERATION_NUMBER + 1]
= COALESCE(balance[ITERATION_NUMBER],principal[1]) * rate[1],
balance[ITERATION_NUMBER+1]
= COALESCE(balance[ITERATION_NUMBER],principal[1])
- payment[1] + interest[ITERATION_NUMBER+1]
)
ORDER BY id, key
Which outputs:
PMT_DT
PAYMENT
INTEREST
REDUCTION
BALANCE
15-AUG-22
1881.8
135
1746.8
7253.2
14-SEP-22
1881.8
108.8
1773.01
5480.19
14-OCT-22
1881.8
82.2
1799.6
3680.59
14-NOV-22
1881.8
55.21
1826.6
1853.99
14-DEC-22
1881.8
27.81
1853.99
0
db<>fiddle here

ORACLE PL/SQL PIVOT procedure

I am trying to write an Oracle PL/SQL procedure to calculate a simple confusion matrix table. I have my labelled data prepared, basically two columns of 0 and 1, actual value vs predicted value.
I was able to calculate it with simple pivot (I think most straightforward option):
SELECT * FROM
( SELECT ACTUAL_VALUE, PREDICTED_VALUE
FROM MY_TABLE
)
PIVOT (
COUNT(PREDICTED_VALUE)
FOR PREDICTED_VALUE IN (1, 0))
ORDER BY ACTUAL_VALUE;
Now I am trying to "plug" all this into a DECLARE, BEGIN... framework but no success. Is it even possible to create procedure to calculate this pivot?
Thanks in advance for any suggestion!
I had to go with cursors and loops. And got my calculations working this way. So closing this one.Plus 3 more summing up calculations at the end.
Thank you
CREATE OR REPLACE PROCEDURE "mydb"."C_MATRIX"
IS
CURSOR MY_DATA IS
SELECT ACTUAL_CARD, PREDICTED_VALUE FROM my_table;
my_data_rec my_data%rowtype;
fp pls_integer := 0;
tp pls_integer := 0;
fn pls_integer := 0;
tn pls_integer := 0;
BEGIN
OPEN my_data;
LOOP
FETCH my_data INTO my_data_rec;
EXIT WHEN my_data%notfound;
IF my_data_rec.ACTUAL_CARD = 0 and my_data_rec.PREDICTED_VALUE = 0 then
tn := tn + 1;
elsif my_data_rec.ACTUAL_CARD = 0 and my_data_rec.PREDICTED_VALUE = 1 then
fn := fn + 1;
elsif my_data_rec.ACTUAL_CARD = 1 and my_data_rec.PREDICTED_VALUE = 1 then
tp := tp + 1;
elsif my_data_rec.ACTUAL_CARD = 1 and my_data_rec.PREDICTED_VALUE = 0 then
fp := fp + 1;
end if;
END LOOP;
dbms_output.put_line
('Number of false Positives: ' ||fp || ' Number of true Positives: ' ||tp || ' Total numbers of records: ' ||(fp + tp) );
dbms_output.put_line
('Number of false Negatives: ' ||fn || ' Number of true Negatives: ' ||tn || ' Total numbers of records: ' ||(fn + tn ));
dbms_output.put_line(' ');
dbms_output.put_line
(' Incorrect predictions: ' || (fp + fn) || ' Correct predictions: ' || (tp + tn) );
dbms_output.put_line(' ');
dbms_output.put_line(' Acurracy: ' || round((((tn + tp) / (tn + tp + fn + fp)) * 100),2) || '%');
dbms_output.put_line(' Precision: ' || round(((tp / (tp + fp)) * 100),2) || '%');
dbms_output.put_line(' Recall: ' || round(((tp / (tp + fn)) * 100),2) || '%');
END;
/

Response time of the query is too high in oracle 11g

SELECT /*+first_rows */ PPS_ID,TOTAL_WEIGHT from
(SELECT PPS_ID,TOTAL_WEIGHT ,row_number() over (order by total_weight desc) row_num
FROM (SELECT pps_id,round((((60 * name_pct_match / 100) + prs_weight + year_weight + dt_weight +
case
when mother_name_pct_match = -1
then
0
else
(10 * mother_name_pct_match / 100)
end)/decode(mother_name_pct_match,-1,(total_attrib_weight - mother_weight),total_attrib_weight)) * 100) total_weight
FROM (SELECT pps_id,
round(func_compare_name('MUHAMMAD YASIN MUHAMMAD ASHRAF',upper(name_en),' ',60)) name_pct_match,
decode(prs_nationality_id, 271, 15, 0) prs_weight,
case
when upper(mother_name_en) in ('MR','MRS','MISS','NISA','M','X')
then -1
else
round(func_compare_name(upper('.'), upper(mother_name_en), ' ',60))
end mother_name_pct_match, 10 mother_weight,
100 total_attrib_weight,
case when to_number(to_char(birth_date, 'yyyy')) = 2007 then 5 else 0 end year_weight,
case when to_char(to_date('01/01/2007','DD-MM-RRRR'), 'dd') = to_char(birth_date, 'dd')
and to_char(to_date('01/01/07','DD-MM-RRRR'), 'mm') = to_char(birth_date, 'mm') then 10
when to_date('01/01/2007','DD-MM-RRRR') between birth_date-6 and birth_date+6 then 8
when to_date('01/01/2007','DD-MM-RRRR') between birth_date-28 and birth_date+28 then 5
when to_date('01/01/2007','DD-MM-RRRR') between birth_date-90 and birth_date+90 then 3
else 0
end dt_weight
FROM INDIV_PROF_sub_test123
WHERE birth_date = '01/01/2007'
AND IS_ACTIVE = 1
AND gender_id = 1
AND round(func_compare_name('MUHAMMAD YASIN MUHAMMAD ASHRAF',upper(name_en),' ',60)) > 20
)
)
WHERE TOTAL_WEIGHT >= 70
)
where row_num <= 10
CREATE OR REPLACE FUNCTION VISION_APP.func_compare_name(p_name_str IN VARCHAR2,
p_str IN VARCHAR2,
p_delim IN VARCHAR2,
p_relev_thresh IN NUMBER)
RETURN NUMBER DETERMINISTIC AS
l_str LONG;
l_n NUMBER;
TYPE mytabletype IS TABLE OF VARCHAR2(255);
l_name_data mytabletype := mytabletype();
l_data mytabletype := mytabletype();
v_name_cnt NUMBER := 0;
v_pct_per_name NUMBER := 0;
v_pct_match NUMBER := 0;
v_flag NUMBER;
v_jaro_pct NUMBER := 0;
v_highest_jaro_pct NUMBER := 0;
v_match_exact NUMBER := 0;
v_match_exact_res NUMBER := 0;
BEGIN
l_str := p_name_str || p_delim;
LOOP
l_n := instr(l_str, p_delim);
EXIT WHEN(nvl(l_n, 0) = 0);
-- condition to check if only space
IF l_n <> 1 THEN
l_name_data.extend;
l_name_data(l_name_data.count) := ltrim(rtrim(substr(l_str,
1,
l_n - 1)));
v_name_cnt := v_name_cnt + 1;
END IF;
l_str := substr(l_str, l_n + length(p_delim));
END LOOP;
v_pct_per_name := 100 / v_name_cnt;
l_str := p_str || p_delim;
LOOP
l_n := instr(l_str, p_delim);
EXIT WHEN(nvl(l_n, 0) = 0);
l_data.extend;
l_data(l_data.count) := ltrim(rtrim(substr(l_str, 1, l_n - 1)));
l_str := substr(l_str, l_n + length(p_delim));
END LOOP;
FOR nme IN 1 .. l_name_data.count LOOP
v_flag := 0;
v_highest_jaro_pct := 0;
FOR i IN 1 .. l_data.count LOOP
v_jaro_pct := utl_match.jaro_winkler_similarity(l_name_data(nme),
l_data(i));
IF soundex(l_name_data(nme)) = soundex(l_data(i)) AND
v_jaro_pct >= p_relev_thresh THEN
IF v_jaro_pct > v_highest_jaro_pct THEN
v_highest_jaro_pct := v_jaro_pct;
END IF;
END IF;
END LOOP;
v_pct_match := v_pct_match +
(v_pct_per_name * v_highest_jaro_pct / 100);
END LOOP;
SELECT utl_match.edit_distance_similarity(p_name_str, p_str)
INTO v_match_exact
FROM dual;
if (trunc(v_match_exact) =100 ) then
return trunc(v_pct_match, 2);
else
if( v_match_exact <> 0) then
v_match_exact_res := (5 / v_match_exact) * 100;
v_pct_match := v_pct_match - v_match_exact_res;
end if;
if v_pct_match >20 then
RETURN trunc(v_pct_match, 2);
end if;
end if;
END;
This query is fetching result from INDIV_PROF_sub_test123 table this is having data of 35 million and partitioned.
i found the problematic area in query func_compare_name we are using it having name similarity checking function
Adding explain plan:
Plan
SELECT STATEMENT HINT: FIRST_ROWS
Cost: 262
Bytes: 4,056
Cardinality: 104 4
VIEW VC_CLONE.
Cost: 262
Bytes: 4,056
Cardinality: 104 3
WINDOW SORT PUSHED RANK
Cost: 262
Bytes: 5,512
Cardinality: 104 2
TABLE ACCESS BY GLOBAL INDEX ROWID TABLE VC_CLONE.INDIV_PROF_ONE_MONTH_1
Cost: 261
Bytes: 5,512
Cardinality: 104
Partition #: 3
Partitions accessed #1672 1
INDEX RANGE SCAN INDEX VC_CLONE.IDX_BIRTH_DT_INVID
Cost: 4
Cardinality: 1 –

PLS-00103: Encountered the symbol ";" when expecting one of the following:

what is wrong in my code
SQL> declare
2 mark number :=50;
3 begin
4 mark :=& mark;
5 if (mark between 85 and 100)
6 then
7 dbms_output.put_line('mark is A ');
8 else if (mark between 50 and 65) then
9 dbms_output.put_line('mark is D ');
10 else if (mark between 66 and 75) then
11 dbms_output.put_line('mark is C ');
12 else if (mark between 76 and 84) then
13 dbms_output.put_line('mark is B');
14 else
15 dbms_output.put_line('mark is F');
16 end if;
17 end;
18 /
Enter value for mark: 65
old 4: mark :=& mark;
new 4: mark :=65;
end;
*
ERROR at line 17:
ORA-06550: line 17, column 4:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
if
The problem is that the else and if are two operators here. Since you open a new 'if' you need a corresponding 'end if'.
Thus:
declare
mark number :=50;
begin
mark :=& mark;
if (mark between 85 and 100) then
dbms_output.put_line('mark is A ');
else
if (mark between 50 and 65) then
dbms_output.put_line('mark is D ');
else
if (mark between 66 and 75) then
dbms_output.put_line('mark is C ');
else
if (mark between 76 and 84) then
dbms_output.put_line('mark is B');
else
dbms_output.put_line('mark is F');
end if;
end if;
end if;
end if;
end;
/
Alternatively you can use elsif:
declare
mark number :=50;
begin
mark :=& mark;
if (mark between 85 and 100)
then
dbms_output.put_line('mark is A ');
elsif (mark between 50 and 65) then
dbms_output.put_line('mark is D ');
elsif (mark between 66 and 75) then
dbms_output.put_line('mark is C ');
elsif (mark between 76 and 84) then
dbms_output.put_line('mark is B');
else
dbms_output.put_line('mark is F');
end if;
end;
/
The IF statement has these forms in PL/SQL:
IF THEN
IF THEN ELSE
IF THEN ELSIF
You have used elseif which in terms of PL/SQL is wrong. That need to be replaced with ELSIF.
DECLARE
mark NUMBER :=50;
BEGIN
mark :=& mark;
IF (mark BETWEEN 85 AND 100) THEN
dbms_output.put_line('mark is A ');
elsif (mark BETWEEN 50 AND 65) THEN
dbms_output.put_line('mark is D ');
elsif (mark BETWEEN 66 AND 75) THEN
dbms_output.put_line('mark is C ');
elsif (mark BETWEEN 76 AND 84) THEN
dbms_output.put_line('mark is B');
ELSE
dbms_output.put_line('mark is F');
END IF;
END;
/

How can I improve Oracle performance when performing a user defined Statistics function?

An associate of mine converted a javascript inverse chi-square routine to Oracle. The good news is that it returns the same results as the javascript routine. The bad news is that where it takes 1.5 seconds to return the result in IE or Chrome, it takes 23 seconds in Oracle. Of that 23 seconds >99% is CPU time.
There are two loops in the routines, an outer loop that executes 36 times for the values we are testing with, and an inner loop that runs 10,753 times for the each iteration of the outer loop. It does the same loop in both JS as it does in Oracle. For each iteration of the inner loop it executes an EXP function and an LN function, both of which are intrinsic in both languages.
I have compiled the Oracle code both Interpreted as well as Native, with little if any change (.045 second difference).
I have three questions;
Why is Oracle so slow/how can I improve it?
Is there an intrinsic inverse chi square function in Oracle.
Does anyone have an inverse chi-square function that does not require the iterative looping (or not as much) as the one I am using?
A bonus question is;
Does anyone have a routine that computes Confidence Intervals in PL/SQL, or a language that could easily be converted to PL/SQL?
As requested, here is the code, which is a bit long (The main routine is CRITCHI for testing P=0.975 and DF=21507.38);
BIGX Number :=20;
FUNCTION POZ(Z IN NUMBER) RETURN NUMBER IS
Y NUMBER;
X NUMBER;
W NUMBER;
Z_MAX NUMBER;
XXX NUMBER;
BEGIN
Z_MAX:=6.0;
IF (Z=0) THEN
X:= 0.0;
ELSE
Y := 0.5 * ABS(Z);
IF (Y >= (Z_MAX * 0.5)) THEN
X:= 1.0;
ELSIF (y < 1.0) THEN
W:= Y * Y;
X:= ((((((((0.000124818987 * W
- 0.001075204047) * W + 0.005198775019) * W
- 0.019198292004) * W + 0.059054035642) * W
- 0.151968751364) * W + 0.319152932694) * W
- 0.531923007300) * W + 0.797884560593) * Y * 2.0;
ELSE
Y:= Y-2.0;
Y:= (((((((((((((-0.000045255659 * Y
+ 0.000152529290) * Y - 0.000019538132) * Y
- 0.000676904986) * Y + 0.001390604284) * Y
- 0.000794620820) * Y - 0.002034254874) * Y
+ 0.006549791214) * Y - 0.010557625006) * Y
+ 0.011630447319) * Y - 0.009279453341) * Y
+ 0.005353579108) * Y - 0.002141268741) * Y
+ 0.000535310849) * Y + 0.999936657524;
END IF;
END IF;
IF (Z>0.0) THEN
XXX:=((X + 1.0) * 0.5);
ELSE
XXX:= ((1.0 - x) * 0.5);
END IF;
RETURN XXX;
END POZ;
FUNCTION EX(X IN NUMBER) RETURN NUMBER IS
BEGIN
IF (x < -BIGX) THEN
RETURN 0;
ELSE
RETURN EXP(X);
END IF;
END EX;
FUNCTION POCHISQ(X IN NUMBER, DF IN NUMBER) RETURN NUMBER IS
A NUMBER;
Y NUMBER;
S NUMBER;
E NUMBER;
C NUMBER;
Z NUMBER;
X1 NUMBER;
EVEN BOOLEAN; /* True if df is an even number */
LOG_SQRT_PI NUMBER := 0.5723649429247000870717135; /* log(sqrt(pi)) */
I_SQRT_PI NUMBER := 0.5641895835477562869480795; /* 1 / sqrt(pi) */
b1 PLS_INTEGER;
b2 PLS_INTEGER;
e1 PLS_INTEGER;
e2 PLS_INTEGER;
BEGIN
b1 := DBMS_UTILITY.GET_TIME();
b2 := DBMS_UTILITY.GET_CPU_TIME();
X1:=X;
IF (X1 <= 0.0 OR DF < 1) THEN
RETURN 1.0;
END IF;
A:= 0.5 * X1;
EVEN:= (MOD(DF,2)=0);
IF (DF > 1) THEN
Y := ex(-A);
END IF;
IF EVEN THEN
S:=Y;
ELSE
S:=(2.0 * poz(-sqrt(X1)));
END IF;
IF (DF > 2) THEN
X1:= 0.5*(DF-1.0);
IF EVEN THEN
Z:=1.0;
ELSE
Z:=0.5;
END IF;
IF (A > BIGX) THEN
IF EVEN THEN
E:=0.0;
ELSE
E:=LOG_SQRT_PI;
END IF;
C:= LN(A);
/* Timming snippet */
e1 := DBMS_UTILITY.GET_TIME() - b1;
e2 := DBMS_UTILITY.GET_CPU_TIME() - b2;
--DBMS_OUTPUT.PUT_LINE( '0-GET_TIME elapsed = ' || e1 || ' hsecs.' );
--DBMS_OUTPUT.PUT_LINE( '0-GET_CPU_TIME elapsed = ' || e2 || ' hsecs.' );
/* End of Timming snippet */
WHILE (Z <= X1)
LOOP
E:= LN(Z) + E;
S:=S+EX(C * Z - A - E);
Z:=Z+1.0;
END LOOP;
e1 := DBMS_UTILITY.GET_TIME() - b1;
e2 := DBMS_UTILITY.GET_CPU_TIME() - b2;
--DBMS_OUTPUT.PUT_LINE( '1-GET_TIME elapsed = ' || e1 || ' hsecs. Z= ' || Z );
--DBMS_OUTPUT.PUT_LINE( '1-GET_CPU_TIME elapsed = ' || e2 || ' hsecs.' );
RETURN S;
ELSE
IF EVEN THEN
E:=1.0;
ELSE
E:=(I_SQRT_PI / sqrt(A));
END IF;
C:= 0.0;
WHILE (Z <= X1)
LOOP
E:= E * (A / Z);
C:= C + E;
Z:=Z+ 1.0;
END LOOP;
e1 := DBMS_UTILITY.GET_TIME() - b1;
e2 := DBMS_UTILITY.GET_CPU_TIME() - b2;
--DBMS_OUTPUT.PUT_LINE( '2-GET_TIME elapsed = ' || e1 || ' hsecs.' );
--DBMS_OUTPUT.PUT_LINE( '2-GET_CPU_TIME elapsed = ' || e2 || ' hsecs.' );
RETURN C * Y + S;
END IF;
ELSE
e1 := DBMS_UTILITY.GET_TIME() - b1;
e2 := DBMS_UTILITY.GET_CPU_TIME() - b2;
--DBMS_OUTPUT.PUT_LINE( '3-GET_TIME elapsed = ' || e1 || ' hsecs.' );
--DBMS_OUTPUT.PUT_LINE( '3-GET_CPU_TIME elapsed = ' || e2 || ' hsecs.' );
RETURN S;
END IF;
END POCHISQ;
/* CRITCHI -- Compute critical chi-square value to
produce given p. We just do a bisection
search for a value within CHI_EPSILON,
relying on the monotonicity of pochisq(). */
FUNCTION CRITCHI(P IN NUMBER, DF IN NUMBER) RETURN NUMBER IS
CHI_EPSILON NUMBER:= 0.000001; /* Accuracy of critchi approximation */
CHI_MAX NUMBER:= 99999.0; /* Maximum chi-square value */
minchisq NUMBER:= 0.0;
maxchisq NUMBER:= CHI_MAX;
chisqval NUMBER;
dummy_count number := 0;
BEGIN
IF (p <= 0.0) THEN
RETURN maxchisq;
ELSE
IF (p >= 1.0) THEN
RETURN 0.0;
END IF;
END IF;
chisqval:= df / sqrt(p); /* fair first value */
WHILE ((maxchisq - minchisq) > CHI_EPSILON)
LOOP
if (pochisq(chisqval, df) < p) THEN
maxchisq:= chisqval;
ELSE
minchisq:= chisqval;
END IF;
chisqval:= (maxchisq + minchisq) * 0.5;
dummy_count := dummy_count + 1;
END LOOP;
--DBMS_OUTPUT.PUT_LINE('chisqval = ' || chisqval);
RETURN chisqval;
END CRITCHI;
For the benefit of future seekers, who might not have the patience to trawl through all the comments, the following optimizations were applied to the program to make it run fast.
All the numeric variables were declared as BINARY_INTEGER(#). Find out more.
Functions were declared as deterministic.
Functions were compiled into native C.
(#) On more modern versions of the database PLS_INTEGER is preferred (simply because BINARY_INTEGER is old and deprecated - it gets converted to PLS_INTEGER under the covers) .
NB - if the OP or #AlexPoole would care to write a similar response, I'll happily upvote their answer and delete this one.

Resources