You are on page 1of 38

Programme using ‘plsql’

CREATE OR REPLACE PROCEDURE p_kp_firstm_proc()


AS
$BODY$
/**************************************************************************
*****
--------------------------------------------------------------------------------
**History**
Date Version No Change Developer
------- ------ ------ ---------
10/30/2022 0.1 Initial version Raghavendra S Sanganaikar
***************************************************************************
******/
DECLARE

P_VAL1 numeric(26,3);
v_loop integer;
v_loop_num1 integer;
boolean_val boolean;
vi_record RECORD;
tmp1 Character varying(60000);
tmp2 Character varying(60000);
tmp3 Character varying(60000);
tmp4 Character varying(60000);
tmp5 Character varying(60000);
tmp6 Character varying(60000);
tmp7 Character varying(60000);
tmp8 Character varying(60000);
tmp9 Character varying(60000);
tmp10 Character varying(60000);

begin

drop table if exists kp_zones_matrix;


create table kp_zones_matrix
as
select source,
round(coalesce(max(zone1),0)) as zone1,
round(coalesce(max(zone2),0)) as zone2,
round(coalesce(max(zone3),0)) as zone3,
round(coalesce(max(zone4),0)) as zone4
from (
select source,
case when destination='zone1'
then distance
end as zone1,
case when destination='zone2'
then distance
end as zone2,
case when destination='zone3'
then distance
end as zone3,
case when destination='zone4'
then distance
end as zone4
from kp_zones a ) b
group by 1
order by 1;

v_loop_num1=0;
v_loop=0;

while v_loop<>-1 loop

tmp1 ='drop table if exists kp_pre_final_matrix'|| v_loop_num1 || ';';


tmp2 ='create table kp_pre_final_matrix'|| v_loop_num1 || '
as
select a.*,
round((zone1+zone2+zone3+zone4)) as base_year_production,
b.distance as hor_prod_dist_vals
from kp_zones_matrix a
full join kp_hor_vals b
on a.source=b.zone
order by source ';

drop table if exists kp_pval_store;


tmp3='create table kp_pval_store
as
select sum(hor_prod_dist_vals)/sum(base_year_production) as uni_val
from kp_pre_final_matrix'|| v_loop_num1 || ' a ';

execute tmp1;
--raise notice '%',tmp1;

execute tmp2;
--raise notice '%',tmp2;

execute tmp3;
--raise notice '%',tmp3;

P_VAL1:=(select uni_val from kp_pval_store) ;


raise notice '%',P_VAL1;

tmp4='drop table if exists final_matrix'|| v_loop_num1 || ';';


tmp5='create table final_matrix'|| v_loop_num1 || '
as
select source,
round(zone1) as zone1,
round(zone2) as zone2,
round(zone3) as zone3,
round(zone4) as zone4,
round((zone1+zone2+zone3+zone4)) as base_year_production,
round(hor_prod_dist_vals) as hor_prod_dist_vals
from (
select source,
'||P_VAL1||' * zone1 as zone1,
'||P_VAL1||' * zone2 as zone2,
'||P_VAL1||' * zone3 as zone3,
'||P_VAL1||' * zone4 as zone4,
hor_prod_dist_vals
from kp_pre_final_matrix'|| v_loop_num1 || ' a
order by source) b ';

drop table if exists kp_fpval_store;


tmp6='create table kp_fpval_store
as
select sum(hor_prod_dist_vals)/sum(base_year_production) as funi_val
from final_matrix'|| v_loop_num1 || ' a ';

tmp7='drop table if exists kp_zones_matrix'|| v_loop_num1 || ';';


tmp8='create table kp_zones_matrix'|| v_loop_num1 || '
as
select * from kp_zones_matrix a ';

execute tmp4;
--raise notice '%',tmp4;

execute tmp5;
--raise notice '%',tmp5;

execute tmp6;
--raise notice '%',tmp2;

P_VAL1:=(select funi_val from kp_fpval_store);


raise notice '%',P_VAL1;

execute tmp7;
--raise notice '%',tmp4;

execute tmp8;
--raise notice '%',tmp5;

boolean_val:=P_VAL1>0.97 and P_VAL1<1.03;


raise notice '%',boolean_val;

drop table if exists kp_zones_matrix;


tmp9='create table kp_zones_matrix
as
select source,
round(zone1) as zone1,
round(zone2) as zone2,
round(zone3) as zone3,
round(zone4) as zone4
from final_matrix'|| v_loop_num1 || ' a ';

execute tmp9;
--raise notice '%',tmp5;

if boolean_val then
v_loop=-1;
end if;

raise notice 'The condition is satisfied exciting while loop ,%',boolean_val;


end loop;

END;
$BODY$
language plpgsql;

commit;

drop table if exists kp_zones;


create table kp_zones
(source varchar,
destination varchar,
distance numeric);

insert into kp_zones values


('zone1','zone2','20'),
('zone1','zone3','40'),
('zone1','zone4','60'),
('zone2','zone1','20'),
('zone2','zone3','50'),
('zone2','zone4','70'),
('zone3','zone1','40'),
('zone3','zone2','50'),
('zone3','zone4','80'),
('zone4','zone1','60'),
('zone4','zone2','70'),
('zone4','zone3','80');
drop table if exists kp_hor_vals;
create table kp_hor_vals
(zone varchar,
distance numeric);

insert into kp_hor_vals values


('zone1','480'),
('zone2','840'),
('zone3','510'),
('zone4','840');
___________________________________________________________________________
Call string:

begin;
call p_kp_firstm_proc();
commit;
OUTPUT
NOTICE: 4.172
NOTICE: 1.000
NOTICE: t
NOTICE: The condition is satisfied exciting while loop ,t
COMMIT
Programme using ‘plsql’

CREATE OR REPLACE PROCEDURE p_kp_secondm_proc()


AS
$BODY$

/*******************************************************************************
--------------------------------------------------------------------------------
**History**
Date Version No Change Developer
------- ------ ------ ---------
11/04/2022 0.1 Initial version Raghavendra S Sanganaikar
**********************************************************************************
/

DECLARE

vi_record RECORD;
tmp1 Character varying(60000);
tmp2 Character varying(60000);
tmp3 Character varying(60000);
tmp4 Character varying(60000);
tmp5 Character varying(60000);
tmp6 Character varying(60000);
tmp7 Character varying(60000);
tmp8 Character varying(60000);
tmp9 Character varying(60000);
tmp10 Character varying(60000);
tmp11 Character varying(60000);
tmp12 Character varying(60000);
tmp13 Character varying(60000);
tmp14 Character varying(60000);
tmp15 Character varying(60000);
tmp16 Character varying(60000);
v_loop_num1 integer;
v_loop_num2 integer;
v_loop integer;
var_gf_col boolean ;
var_gf_row boolean;
begin

drop table if exists kp_zones_matrix1;


create table kp_zones_matrix1
as
select source,
round(coalesce(max(zone1),0)) as zone1,
round(coalesce(max(zone2),0)) as zone2,
round(coalesce(max(zone3),0)) as zone3,
round(coalesce(max(zone4),0)) as zone4
from (
select source,
case when destination='zone1'
then distance
end as zone1,
case when destination='zone2'
then distance
end as zone2,
case when destination='zone3'
then distance
end as zone3,
case when destination='zone4'
then distance
end as zone4
from kp_zones1) a
group by 1;

drop table if exists kp_pre_final_matrix1;


create table kp_pre_final_matrix1
as
select a.*,
round((zone1+zone2+zone3+zone4)) as obase_year_production,
b.gfactor as gp,
round((zone1+zone2+zone3+zone4)) * b.gfactor as obase_hor
from kp_zones_matrix1 a
full join kp_hor_gfs0 b
on a.source=b.zone

union all

select 'obase_year_production',
round(sum(zone1)) as zone1,
round(sum(zone2)) as zone2,
round(sum(zone3)) as zone3,
round(sum(zone4)) as zone4,
null,
null,
null
from kp_zones_matrix1

union all

select 'gf',
round((select gfactor::numeric(26,3) from kp_hor_gfs0 where zone='zone1')::numeric(26,3),3)
as zone1,
round((select gfactor::numeric(26,3) from kp_hor_gfs0 where
zone='zone2')::numeric(26,3),3) as zone2,
round((select gfactor::numeric(26,3) from kp_hor_gfs0 where
zone='zone3')::numeric(26,3),3) as zone3,
round((select gfactor::numeric(26,3) from kp_hor_gfs0 where
zone='zone4')::numeric(26,3),3) as zone4,
null,
null,
null
order by source;

insert into kp_pre_final_matrix1


(source,
zone1,
zone2,
zone3,
zone4)
select 'dbase_hor' as source,
max(case when source='obase_year_production'
then zone1*(select gfactor::numeric(26,3) from kp_hor_gfs0 where zone='zone1') end )
as zone1,
max(case when source='obase_year_production'
then zone2*(select gfactor::numeric(26,3) from kp_hor_gfs0 where zone='zone2') end )
as zone2,
max(case when source='obase_year_production'
then zone3*(select gfactor::numeric(26,3) from kp_hor_gfs0 where zone='zone3') end )
as zone3,
max(case when source='obase_year_production'
then zone4*(select gfactor::numeric(26,3) from kp_hor_gfs0 where zone='zone4') end )
as zone4
from kp_pre_final_matrix1 a;

v_loop_num1=0;
v_loop_num2=0;
v_loop=0;

while v_loop<>-1 loop

tmp1 = 'drop table if exists kp_gather_gfs'|| v_loop_num1 || ';';


tmp2 = 'create table kp_gather_gfs'|| v_loop_num1 || '
as
select a.source,
round(b.gfactor::numeric(26,3),3) as gfs,
round(d.gfactor::numeric(26,3),3) as gfd,
c.source as dest
from kp_zones_matrix1 a
full join kp_hor_gfs'|| v_loop_num2 || ' b
on b.zone=a.source
join kp_zones_matrix1 c
on b.zone<>c.source
join kp_hor_gfs'|| v_loop_num2 || ' d
on d.zone=c.source ' ;

tmp3 = 'drop table if exists kp_pre_iteration'|| v_loop_num1 || ';';


tmp4 = 'create table kp_pre_iteration'|| v_loop_num1 || '
as
select gfs,
gfd,
b.distance,
round(((gfs+gfd)/2)*b.distance) as new_matrix_vals,
a.source ,
a.dest
from kp_gather_gfs'|| v_loop_num1 || ' a
join kp_zones1 b
on a.source=b.source
and a.dest=b.destination';

tmp5='drop table if exists kp_pre_iteration1'|| v_loop_num1 || ';';


tmp6='create table kp_pre_iteration1'|| v_loop_num1 || '
as
select source,
coalesce(zone1,0) as zone1,
coalesce(zone2,0) as zone2,
coalesce(zone3,0) as zone3,
coalesce(zone4,0) as zone4,
(coalesce(zone1,0)+coalesce(zone2,0)+coalesce(zone3,0)+coalesce(zone4,0)) as
org_sum
from (
select source,
max(case when dest=''zone1''
then new_matrix_vals
end)zone1,
max(case when dest=''zone2''
then new_matrix_vals
end)zone2,
max(case when dest=''zone3''
then new_matrix_vals
end)zone3,
max(case when dest=''zone4''
then new_matrix_vals
end)zone4
from kp_pre_iteration'|| v_loop_num1 || ' a
where source=''zone1''
group by 1
union all
select source,
max(case when dest=''zone1''
then new_matrix_vals
end)zone1,
max(case when dest=''zone2''
then new_matrix_vals
end)zone2,
max(case when dest=''zone3''
then new_matrix_vals
end)zone3,
max(case when dest=''zone4''
then new_matrix_vals
end)zone4
from kp_pre_iteration'|| v_loop_num1 || ' b
where source=''zone2''
group by 1
union all
select source,
max(case when dest=''zone1''
then new_matrix_vals
end)zone1,
max(case when dest=''zone2''
then new_matrix_vals
end)zone2,
max(case when dest=''zone3''
then new_matrix_vals
end)zone3,
max(case when dest=''zone4''
then new_matrix_vals
end)zone4 from
kp_pre_iteration'|| v_loop_num1 || ' c
where source=''zone3''
group by 1
union all
select source,
max(case when dest=''zone1''
then new_matrix_vals
end)zone1,
max(case when dest=''zone2''
then new_matrix_vals
end)zone2,
max(case when dest=''zone3''
then new_matrix_vals
end)zone3,
max(case when dest=''zone4''
then new_matrix_vals
end)zone4 from
kp_pre_iteration'|| v_loop_num1 || ' c
where source=''zone4''
group by 1
order by source) a ';

tmp7='drop table if exists kp_pre_final_matrix2'|| v_loop_num1 || ';';


tmp8='create table kp_pre_final_matrix2'|| v_loop_num1 || '
as
select source ,
round(zone1) as zone1,
round(zone2) as zone2,
round(zone3) as zone3,
round(zone4) as zone4,
round(org_sum) as org_sum,
round((obase_hor/org_sum)::numeric(26,3),3) as new_gf,
round(obase_hor) as obase_hor
from (
select a.source,
a.zone1 ,
a.zone2,
a.zone3,
a.zone4,
a.org_sum,
b.obase_hor as obase_hor
from kp_pre_iteration1'|| v_loop_num1 || ' a
left join kp_pre_final_matrix1 b
on a.source=b.source

union all

select ''dest_sum'' as source ,


sum(coalesce(zone1,0)) as zone1 ,
sum(coalesce(zone2,0)) as zone2,
sum(coalesce(zone3,0)) as zone3,
sum(coalesce(zone4,0)) as zone4,
null,
null
from kp_pre_iteration1'|| v_loop_num1 || ' c

union all

select ''dbase_hor_const'' as source,


zone1,
zone2,
zone3,
zone4,
null,
null
from kp_pre_final_matrix1 s
where source=''dbase_hor'') a ';

tmp9='drop table if exists kp_final_matrix'|| v_loop_num1 || ';';


tmp10='create table kp_final_matrix'|| v_loop_num1 || '
as
select source,
zone1,
zone2,
zone3,
zone4,
org_sum,
obase_hor,
round(new_gf::numeric(26,3),3) as new_gf
from kp_pre_final_matrix2'|| v_loop_num1 || ' b

union all

select ''new_hor_gf'' as source ,


round(max(case when source=''dbase_hor_const''
then zone1/(select zone1 from kp_pre_final_matrix2'|| v_loop_num1 || ' where
source=''dest_sum'')::numeric(26,3) end )::numeric(26,3),3) as zone1,
round(max(case when source=''dbase_hor_const''
then zone2/(select zone2 from kp_pre_final_matrix2'|| v_loop_num1 || ' where
source=''dest_sum'')::numeric(26,3) end )::numeric(26,3),3) as zone2,
round(max(case when source=''dbase_hor_const''
then zone3/(select zone3 from kp_pre_final_matrix2'|| v_loop_num1 || ' where
source=''dest_sum'')::numeric(26,3) end )::numeric(26,3),3) as zone3,
round(max(case when source=''dbase_hor_const''
then zone4/(select zone4 from kp_pre_final_matrix2'|| v_loop_num1 || ' where
source=''dest_sum'')::numeric(26,3) end )::numeric(26,3),3) as zone4,
null,
null,
null
from kp_pre_final_matrix2'|| v_loop_num1 || ' a';

execute tmp1;
-- raise notice '%',tmp1;

execute tmp2;
--raise notice '%',tmp2;

execute tmp3;
--raise notice '%',tmp3;

execute tmp4;
----raise notice '%',tmp4;

execute tmp5;
--raise notice '%',tmp5;

execute tmp6;
---raise notice '%',tmp6;

execute tmp7;
--raise notice '%',tmp7;

execute tmp8;
--raise notice '%',tmp8;

execute tmp9;
--raise notice '%',tmp9;

execute tmp10;
--raise notice '%',tmp10;

v_loop_num2=v_loop_num2+1;
raise notice '%',v_loop_num2;

tmp11 = 'drop table if exists kp_hor_gfs'|| v_loop_num2 || ';';


tmp12 = 'create table kp_hor_gfs'|| v_loop_num2 || '
as
select new_gf as gfactor,
source as zone
from kp_final_matrix'|| v_loop_num1 ||' a
where source in (''zone1'',''zone2'',''zone3'',''zone4'')';

execute tmp11;
--raise notice '%',tmp11;

execute tmp12;
--raise notice '%',tmp12;

drop table if exists kp_zones_matrix1;


tmp13 ='create table kp_zones_matrix1
as
select source,
zone1,
zone2,
zone3,
zone4
from kp_final_matrix'|| v_loop_num1 || ' a
where source in (''zone1'',''zone2'',''zone3'',''zone4'')';

execute tmp13;
-- raise notice '%',tmp13;

drop table if exists kp_zones1;


tmp14='create table kp_zones1
as
select source,
dest as destination,
new_matrix_vals as distance
from kp_pre_iteration'|| v_loop_num1 || ' a ';

execute tmp14;
--raise notice '%',tmp14;

drop table if exists kp_check_gfs_rows_1;


tmp15='create table kp_check_gfs_rows_1
as
select case when zone1>=''0.97'' and zone1<=''1.03'' and zone2>=''0.97'' and zone2<=''1.03''
and zone3>=''0.97'' and zone3<=''1.03'' and zone4>=''0.97'' and zone4<=''1.03''
then true
else false
end as final_op
from kp_final_matrix'|| v_loop_num1 ||' a
where source=''new_hor_gf'';';

execute tmp15;

--raise notice '%',tmp19;


var_gf_row:=(select final_op from kp_check_gfs_rows_1);

raise notice 'row boolean value is %',var_gf_row;

drop table if exists kp_check_gfs_columns_1;


tmp16='create table kp_check_gfs_columns_1
as
select case when zone1>=''0.97'' and zone1<=''1.03'' and zone2>=''0.97'' and zone2<=''1.03''
and zone3>=''0.97'' and zone3<=''1.03'' and zone4>=''0.97'' and zone4<=''1.03''
then true
else false
end as final_op
from (
select max(case when source=''zone1'' then new_gf end ) zone1,
max(case when source=''zone2'' then new_gf end ) zone2,
max(case when source=''zone3'' then new_gf end ) zone3,
max(case when source=''zone4'' then
new_gf end ) zone4
from kp_final_matrix'|| v_loop_num1 ||' a) b ';

execute tmp16;
--raise notice '%',tmp16;

var_gf_col:=(select final_op from kp_check_gfs_columns_1);

raise notice 'column boolean value is %',var_gf_col;

if var_gf_row and var_gf_col then


v_loop=-1;
end if;

v_loop_num1=v_loop_num1+1;
raise notice '%',v_loop_num1;

end LOOP;

END;
$BODY$
language plpgsql;

commit;
drop table if exists kp_zones1;
create table kp_zones1
(source varchar,
destination varchar,
distance numeric);

insert into kp_zones1 values


('zone1','zone2','20'),
('zone1','zone3','40'),
('zone1','zone4','60'),
('zone2','zone1','20'),
('zone2','zone3','50'),
('zone2','zone4','70'),
('zone3','zone1','40'),
('zone3','zone2','50'),
('zone3','zone4','80'),
('zone4','zone1','60'),
('zone4','zone2','70'),
('zone4','zone3','80');

drop table if exists kp_hor_gfs0;


create table kp_hor_gfs0
(zone varchar,
gfactor numeric(26,3));

insert into kp_hor_gfs0 values


('zone1','2.0'),
('zone2','3.0'),
('zone3','4.0'),
('zone4','5.0');
Call string:

begin;
call p_kp_secondm_proc();
commit;
OUTPUT
NOTICE: 1
NOTICE: row boolean value is f
NOTICE: column boolean value is f
NOTICE: 1
NOTICE: 2
NOTICE: row boolean value is f
NOTICE: column boolean value is f
NOTICE: 2
NOTICE: 3
NOTICE: row boolean value is f
NOTICE: column boolean value is f
NOTICE: 3
NOTICE: 4
NOTICE: row boolean value is f
NOTICE: column boolean value is f
NOTICE: 4
NOTICE: 5
NOTICE: row boolean value is f
NOTICE: column boolean value is f
NOTICE: 5
NOTICE: 6
NOTICE: row boolean value is f
NOTICE: column boolean value is f
NOTICE: 6
NOTICE: 7
NOTICE: row boolean value is f
NOTICE: column boolean value is f
NOTICE: 7
NOTICE: 8
NOTICE: row boolean value is f
NOTICE: column boolean value is f
NOTICE: 8
NOTICE: 9
NOTICE: row boolean value is f
NOTICE: column boolean value is f
NOTICE: 9
NOTICE: 10
NOTICE: row boolean value is f
NOTICE: column boolean value is f
NOTICE: 10
NOTICE: 11
NOTICE: row boolean value is t
NOTICE: column boolean value is t
NOTICE: 11
COMMIT
Programme using ‘plsql’

CREATE OR REPLACE PROCEDURE p_kp_dm_proc()


AS
$BODY$

/*******************************************************************************
--------------------------------------------------------------------------------
**History**
Date Version No Change Developer
------- ------ ------ ---------
11/04/2022 0.1 Initial version Raghavendra S Sanganaikar
*********************************************************************************/
DECLARE
var_f numeric(26,3);
var_gf_col boolean ;
var_gf_row boolean;
v_loop integer;
var_f1 varchar;
vi_record RECORD;
tmp1 Character varying(60000);
tmp2 Character varying(60000);
tmp3 Character varying(60000);
tmp4 Character varying(60000);
tmp5 Character varying(60000);
tmp6 Character varying(60000);
tmp7 Character varying(60000);
tmp8 Character varying(60000);
tmp9 Character varying(60000);
tmp10 Character varying(60000);
tmp11 Character varying(60000);
tmp12 Character varying(60000);
tmp13 Character varying(60000);
tmp14 Character varying(60000);
tmp15 Character varying(60000);
tmp16 Character varying(60000);
tmp17 Character varying(60000);
tmp18 Character varying(60000);
tmp19 Character varying(60000);
tmp20 Character varying(60000);
v_loop_num1 integer;
v_loop_num2 integer;

begin

drop table if exists kp_zonal_matrix_level1;


create table kp_zonal_matrix_level1
as
select source,
round(coalesce(max(zone1),0)) as zone1,
round(coalesce(max(zone2),0)) as zone2,
round(coalesce(max(zone3),0)) as zone3,
round(coalesce(max(zone4),0)) as zone4
from (
select source,
case when destination='zone1'
then distance
end as zone1,
case when destination='zone2'
then distance
end as zone2,
case when destination='zone3'
then distance
end as zone3,
case when destination='zone4'
then distance
end as zone4
from kp_zones_list b ) a
group by 1;

drop table if exists kp_zonal_matrix_level2;


create table kp_zonal_matrix_level2
as
select a.*,
round((zone1+zone2+zone3+zone4)) as oibase_year_production,
b.gfactor as gf,
round((zone1+zone2+zone3+zone4))* b.gfactor as oi_horiz
from kp_zonal_matrix_level1 a
full join kp_given_gf0 b
on a.source=b.zone

union all

select 'djbase_year_production' as source,


round(sum(zone1)) as zone1,
round(sum(zone2)) as zone2,
round(sum(zone3)) as zone3,
round(sum(zone4)) as zone4,
null,
null,
null
from kp_zonal_matrix_level1 c

union all

select 'gf',
round((select gfactor::numeric(26,3) from kp_given_gf0 e where
zone='zone1')::numeric(26,3),3) as zone1,
round((select gfactor::numeric(26,3) from kp_given_gf0 f where
zone='zone2')::numeric(26,3),3) as zone2,
round((select gfactor::numeric(26,3) from kp_given_gf0 g where
zone='zone3')::numeric(26,3),3) as zone3,
round((select gfactor::numeric(26,3) from kp_given_gf0 h where
zone='zone4')::numeric(26,3),3) as zone4,
null,
null,
null
order by source;

insert into kp_zonal_matrix_level2


(source,
zone1,
zone2,
zone3,
zone4)
select 'djbase_hor' as source,
round(max(case when source='djbase_year_production'
then zone1*(select gfactor::numeric(26,3) from kp_given_gf0 b where
zone='zone1') end )) as zone1,
round(max(case when source='djbase_year_production'
then zone2*(select gfactor::numeric(26,3) from kp_given_gf0 c where
zone='zone2') end )) as zone2,
round(max(case when source='djbase_year_production'
then zone3*(select gfactor::numeric(26,3) from kp_given_gf0 d where
zone='zone3') end )) as zone3,
round(max(case when source='djbase_year_production'
then zone4*(select gfactor::numeric(26,3) from kp_given_gf0 e where
zone='zone4') end )) as zone4
from kp_zonal_matrix_level2 a;

var_f:=(select round(sum(oi_horiz)/sum(oibase_year_production),3) from


kp_zonal_matrix_level2) ;
raise notice '%',var_f;

v_loop_num1=0;
v_loop_num2=0;
v_loop=0;

while v_loop<>-1 loop

tmp1 ='drop table if exists kp_gather_gfs_dm'|| v_loop_num1 || ';';


tmp2 ='create table kp_gather_gfs_dm'|| v_loop_num1 || '
as
select a.source,
round(b.gfactor::numeric(26,3),3) as gfs,
round(d.gfactor::numeric(26,3),3) as gfd,
c.source as dest
from kp_zonal_matrix_level1 a
full join kp_given_gf'|| v_loop_num2 || ' b
on b.zone=a.source
join kp_zonal_matrix_level1 c
on b.zone<>c.source
join kp_given_gf'|| v_loop_num2 || ' d
on d.zone=c.source ';

tmp4 = 'drop table if exists kp_pre_iteration_dm'|| v_loop_num1 || ';';


tmp5 = 'create table kp_pre_iteration_dm'|| v_loop_num1 || '
as
select gfs,
gfd,
b.distance,
round(((gfs*gfd)/'||var_f||')*b.distance) as new_matrix_vals,
a.source ,
a.dest
from kp_gather_gfs_dm'|| v_loop_num1 || ' a
join kp_zones_list b
on a.source=b.source
and a.dest=b.destination';

tmp6='drop table if exists kp_pre_iteration_dm_level2'|| v_loop_num1 || ';';


tmp7='create table kp_pre_iteration_dm_level2'|| v_loop_num1 || '
as
select source,
coalesce(zone1,0) as zone1,
coalesce(zone2,0) as zone2,
coalesce(zone3,0) as zone3,
coalesce(zone4,0) as zone4,
(coalesce(zone1,0)+coalesce(zone2,0)+coalesce(zone3,0)+coalesce(zone4,0)) as
oi_curr
from (select source,
max(case when dest=''zone1''
then new_matrix_vals
end)zone1,
max(case when dest=''zone2''
then new_matrix_vals
end)zone2,
max(case when dest=''zone3''
then new_matrix_vals
end)zone3,
max(case when dest=''zone4''
then new_matrix_vals
end)zone4
from kp_pre_iteration_dm'|| v_loop_num1 || ' a
where source=''zone1''
group by 1

union all

select source,
max(case when dest=''zone1''
then new_matrix_vals
end)zone1,
max(case when dest=''zone2''
then new_matrix_vals
end)zone2,
max(case when dest=''zone3''
then new_matrix_vals
end)zone3,
max(case when dest=''zone4''
then new_matrix_vals
end)zone4
from kp_pre_iteration_dm'|| v_loop_num1 || ' b
where source=''zone2''
group by 1

union all

select source,
max(case when dest=''zone1''
then new_matrix_vals
end)zone1,
max(case when dest=''zone2''
then new_matrix_vals
end)zone2,
max(case when dest=''zone3''
then new_matrix_vals
end)zone3,
max(case when dest=''zone4''
then new_matrix_vals
end)zone4
from kp_pre_iteration_dm'|| v_loop_num1 || ' c
where source=''zone3''
group by 1

union all

select source,
max(case when dest=''zone1''
then new_matrix_vals
end)zone1,
max(case when dest=''zone2''
then new_matrix_vals
end)zone2,
max(case when dest=''zone3''
then new_matrix_vals
end)zone3,
max(case when dest=''zone4''
then new_matrix_vals
end)zone4
from kp_pre_iteration_dm'|| v_loop_num1 || ' d
where source=''zone4''
group by 1
order by source )a ';

tmp8='drop table if exists kp_pre_iteration_dm_level3'|| v_loop_num1 || ';';


tmp9='create table kp_pre_iteration_dm_level3'|| v_loop_num1 || '
as
select source ,
round(zone1) as zone1,
round(zone2) as zone2,
round(zone3) as zone3,
round(zone4) as zone4,
round(oi_curr) as oi_curr,
round((oi_horiz/oi_curr)::numeric(26,3),3) as new_gf,
oi_horiz
from (
select a.source,
a.zone1 ,
a.zone2,
a.zone3,
a.zone4,
a.oi_curr,
round(b.oi_horiz) as oi_horiz
from kp_pre_iteration_dm_level2'|| v_loop_num1 || ' a
left join kp_zonal_matrix_level2 b
on a.source=b.source) c

union all

select ''dj_curr'' as source ,


sum(coalesce(zone1,0)) as zone1 ,
sum(coalesce(zone2,0)) as zone2,
sum(coalesce(zone3,0)) as zone3,
sum(coalesce(zone4,0)) as zone4,
null,
null,
null
from kp_pre_iteration_dm_level2'|| v_loop_num1 || ' d

union all

select ''dj_horiz'' as source,


round(zone1) as zone1,
round(zone2) as zone2,
round(zone3) as zone3,
round(zone4) as zone4,
null,
null,
null
from kp_zonal_matrix_level2 e
where source=''djbase_hor''';

tmp10='drop table if exists kp_final_matrix_dm'|| v_loop_num1 || ';';


tmp11='create table kp_final_matrix_dm'|| v_loop_num1 || '
as
select source,
zone1,
zone2,
zone3,
zone4,
oi_curr,
oi_horiz,
round(new_gf::numeric(26,3),3) as new_gf
from kp_pre_iteration_dm_level3'|| v_loop_num1 || ' b

union all

select ''new_gf'' as source ,


round(max(case when source=''dj_horiz''
then zone1/(select zone1 from kp_pre_iteration_dm_level3'|| v_loop_num1 || '
c where source=''dj_curr'')::numeric(26,3) end )::numeric(26,3),3) as zone1,
round(max(case when source=''dj_horiz''
then zone2/(select zone2 from kp_pre_iteration_dm_level3'|| v_loop_num1 || '
d where source=''dj_curr'')::numeric(26,3) end )::numeric(26,3),3) as zone2,
round(max(case when source=''dj_horiz''
then zone3/(select zone3 from kp_pre_iteration_dm_level3'|| v_loop_num1 || '
e where source=''dj_curr'')::numeric(26,3) end )::numeric(26,3),3) as zone3,
round(max(case when source=''dj_horiz''
then zone4/(select zone4 from kp_pre_iteration_dm_level3'|| v_loop_num1 || ' f
where source=''dj_curr'')::numeric(26,3) end )::numeric(26,3),3) as zone4,
null,
null,
null
from kp_pre_iteration_dm_level3'|| v_loop_num1 || ' a
order by source';

execute tmp1;
--raise notice '%',tmp1;

execute tmp2;
--raise notice '%',tmp2;

execute tmp4;
--raise notice '%',tmp4;

execute tmp5;
--raise notice '%',tmp5;

execute tmp6;
--raise notice '%',tmp6;

execute tmp7;
--raise notice '%',tmp7;

execute tmp8;
--raise notice '%',tmp8;
execute tmp9;
--raise notice '%',tmp9;

execute tmp10;
--raise notice '%',tmp10;

execute tmp11;
--raise notice '%',tmp11;

v_loop_num2=v_loop_num2+1;
raise notice '%',v_loop_num2;

tmp12 = 'drop table if exists kp_given_gf'|| v_loop_num2 || ';';


tmp13 = 'create table kp_given_gf'|| v_loop_num2 || '
as
select new_gf as gfactor,
source as zone
from kp_final_matrix_dm'|| v_loop_num1 ||' a
where source in (''zone1'',''zone2'',''zone3'',''zone4'')';

execute tmp12;
--raise notice '%',tmp12;

execute tmp13;
--raise notice '%',tmp13;

drop table if exists kp_zonal_matrix_level1;


tmp15 ='create table kp_zonal_matrix_level1
as
select source,
zone1,
zone2,
zone3,
zone4
from kp_final_matrix_dm'|| v_loop_num1 || ' b
where source in (''zone1'',''zone2'',''zone3'',''zone4'')';

--execute tmp14;
--raise notice '%',tmp14;

execute tmp15;
--raise notice '%',tmp15;

drop table if exists kp_zones_list;


tmp17='create table kp_zones_list
as
select source,
dest as destination,
new_matrix_vals as distance
from kp_pre_iteration_dm'|| v_loop_num1 ||';';
execute tmp17;
--raise notice '%',tmp17;

drop table if exists kp_new_ffactor;


tmp18='create table kp_new_ffactor
as
select round(sum(oi_horiz)/sum(oi_curr),3) as new_ffactor
from kp_final_matrix_dm'|| v_loop_num1 ||';';

execute tmp18;
--raise notice '%',tmp18;

var_f:=(select new_ffactor from kp_new_ffactor);


raise notice '%',var_f;

drop table if exists kp_check_gfs_rows;


tmp19='create table kp_check_gfs_rows
as
select case when zone1>=''0.97'' and zone1<=''1.03'' and zone2>=''0.97'' and zone2<=''1.03''
and zone3>=''0.97'' and zone3<=''1.03''
and zone4>=''0.97'' and zone4<=''1.03''
then true
else false
end as final_op
from kp_final_matrix_dm'|| v_loop_num1 ||' a
where source=''new_gf'';';

execute tmp19;
--raise notice '%',tmp19;

var_gf_row:=(select final_op from kp_check_gfs_rows);

raise notice 'row boolean value is %',var_gf_row;

drop table if exists kp_check_gfs_columns;


tmp20='create table kp_check_gfs_columns
as
select case when zone1>=''0.97'' and zone1<=''1.03'' and zone2>=''0.97'' and zone2<=''1.03''
and zone3>=''0.97'' and zone3<=''1.03'' and zone4>=''0.97'' and zone4<=''1.03''
then true
else false
end as final_op
from (
select max(case when source=''zone1'' then new_gf end ) zone1,
max(case when source=''zone2'' then new_gf end ) zone2,
max(case when source=''zone3'' then new_gf end ) zone3,
max(case when source=''zone4'' then new_gf end ) zone4
from kp_final_matrix_dm'|| v_loop_num1 ||' b ) a';

execute tmp20;
--raise notice '%',tmp20;
var_gf_col:=(select final_op from kp_check_gfs_columns);

raise notice 'column boolean value is %',var_gf_col;

if var_gf_col and var_gf_row then


v_loop=-1;
end if;

v_loop_num1=v_loop_num1+1;
raise notice '%',v_loop_num1;

end loop;

END;
$BODY$
language plpgsql;

commit;

begin;
call p_kp_dm_proc();
commit;
drop table if exists kp_zones_list;
create table kp_zones_list
(source varchar,
destination varchar,
distance numeric);

insert into kp_zones_list values


('zone1','zone2','45'),
('zone1','zone3','65'),
('zone1','zone4','80'),
('zone2','zone1','45'),
('zone2','zone3','110'),
('zone2','zone4','125'),
('zone3','zone1','65'),
('zone3','zone2','110'),
('zone3','zone4','90'),
('zone4','zone1','80'),
('zone4','zone2','125'),
('zone4','zone3','90');

drop table if exists kp_given_gf0;


create table kp_given_gf0
(zone varchar,
gfactor numeric(26,3));

insert into kp_given_gf0 values


('zone1','1.0'),
('zone2','2.0'),
('zone3','4.0'),
('zone4','3.0');
Call string:

begin;
call p_kp_dm_proc();
commit;
OUTPUT
WARNING: there is no transaction in progress
NOTICE: 2.617
NOTICE: 1
NOTICE: 1.069
NOTICE: row boolean value is f
NOTICE: column boolean value is f
NOTICE: 1
NOTICE: 2
NOTICE: 1.012
NOTICE: row boolean value is f
NOTICE: column boolean value is f
NOTICE: 2
NOTICE: 3
NOTICE: 1.003
NOTICE: row boolean value is f
NOTICE: column boolean value is f
NOTICE: 3
NOTICE: 4
NOTICE: 1.001
NOTICE: row boolean value is t
NOTICE: column boolean value is t
NOTICE: 4
COMMIT
Programme using ‘plsql’

CREATE OR REPLACE PROCEDURE p_kp_fm_proc()


AS
$BODY$

/*******************************************************************************
--------------------------------------------------------------------------------
**History**
Date Version No Change Developer
------- ------ ------ ---------
11/06/2022 0.1 Initial version Raghavendra S Sanganaikar
*********************************************************************************/

DECLARE

tmp1 Character varying(60000);


tmp2 Character varying(60000);
tmp3 Character varying(60000);
tmp4 Character varying(60000);
tmp5 Character varying(60000);
tmp6 Character varying(60000);
tmp7 Character varying(60000);
tmp8 Character varying(60000);
tmp9 Character varying(60000);
tmp10 Character varying(60000);
tmp11 Character varying(60000);
tmp12 Character varying(60000);
tmp13 Character varying(60000);
tmp14 Character varying(60000);
tmp15 Character varying(60000);
tmp16 Character varying(60000);
v_loop_num1 integer;
v_loop_num2 integer;
v_loop integer;
var_gf_col boolean ;
var_gf_row boolean;

begin

drop table if exists kp_zones_matrix_fm1;


create table kp_zones_matrix_fm1
as
select source,
round(coalesce(max(zone1),0)) as zone1,
round(coalesce(max(zone2),0)) as zone2,
round(coalesce(max(zone3),0)) as zone3,
round(coalesce(max(zone4),0)) as zone4
from (
select source,
case when destination='zone1'
then distance
end as zone1,
case when destination='zone2'
then distance
end as zone2,
case when destination='zone3'
then distance
end as zone3,
case when destination='zone4'
then distance
end as zone4
from kp_zones_fm) a
group by 1
order by 1;

drop table if exists kp_pre_final_matrix_dm1;


create table kp_pre_final_matrix_dm1
as
select a.*,
round((zone1+zone2+zone3+zone4)) as oi_curr,
b.gfactor as fi,
round(round((zone1+zone2+zone3+zone4)) * b.gfactor) as oi_hor
from kp_zones_matrix_fm1 a
full join kp_gfsfm0 b
on a.source=b.zone

union all

select 'dj_curr',
round(sum(zone1)) as zone1,
round(sum(zone2)) as zone2,
round(sum(zone3)) as zone3,
round(sum(zone4)) as zone4,
null,
null,
null
from kp_zones_matrix_fm1 fm

union all

select 'dj_hor',
round(round(sum(zone1)) * (select b.gfactor from kp_gfsfm0 b where b.zone='zone1')),
round(round(sum(zone2)) * (select b.gfactor from kp_gfsfm0 b where b.zone='zone2')),
round(round(sum(zone3)) * (select b.gfactor from kp_gfsfm0 b where b.zone='zone3')),
round(round(sum(zone4)) * (select b.gfactor from kp_gfsfm0 b where b.zone='zone4')),
null,
null,
null
from kp_zones_matrix_fm1 fm1;

drop table if exists kp_base_matrix;


create table kp_base_matrix
as
select * from kp_pre_final_matrix_dm1;

v_loop_num1=0;
v_loop_num2=0;
v_loop=0;

while v_loop<>-1 loop

tmp1 = 'drop table if exists kp_final_matrix_step1_dm1'|| v_loop_num1 || ';';


tmp2 = 'create table kp_final_matrix_step1_dm1'|| v_loop_num1 || '
as
select source,
round(zone1*fi) as zone1,
round(zone2*fi) as zone2,
round(zone3*fi) as zone3,
round(zone4*fi) as zone4
from kp_pre_final_matrix_dm1 dm
where source in (''zone1'',''zone2'',''zone3'',''zone4'')' ;

tmp3 = 'drop table if exists kp_final_matrix_step1_dm2'|| v_loop_num1 || ';';


tmp4 = 'create table kp_final_matrix_step1_dm2'|| v_loop_num1 || '
as
select a.*
from kp_final_matrix_step1_dm1'|| v_loop_num1 || ' a

union all

select ''dj_curr'' as source,


sum(zone1) as zone1,
sum(zone2) as zone2,
sum(zone3) as zone3,
sum(zone4) as zone4
from kp_final_matrix_step1_dm1'|| v_loop_num1 || ' b
group by 1

union all

select source,
zone1,
zone2,
zone3,
zone4
from kp_base_matrix
where source=''dj_hor''';

tmp5 = 'drop table if exists kp_final_matrix_step1_dm'|| v_loop_num1 || ';';


tmp6 = 'create table kp_final_matrix_step1_dm'|| v_loop_num1 || '
as
select * from kp_final_matrix_step1_dm2'|| v_loop_num1 || ' a
union all

select ''fj'' as source,


round(max(case when source=''dj_hor''
then zone1/(select zone1 from kp_final_matrix_step1_dm2'|| v_loop_num1 || '
a where source=''dj_curr'')::numeric(26,3) end )::numeric(26,3),3) as zone1,
round(max(case when source=''dj_hor''
then zone2/(select zone2 from kp_final_matrix_step1_dm2'|| v_loop_num1 || '
b where source=''dj_curr'')::numeric(26,3) end )::numeric(26,3),3) as zone2,
round(max(case when source=''dj_hor''
then zone3/(select zone3 from kp_final_matrix_step1_dm2'|| v_loop_num1 || '
c where source=''dj_curr'')::numeric(26,3) end )::numeric(26,3),3) as zone3,
round(max(case when source=''dj_hor''
then zone4/(select zone4 from kp_final_matrix_step1_dm2'|| v_loop_num1 || '
d where source=''dj_curr'')::numeric(26,3) end )::numeric(26,3),3) as zone4
from kp_final_matrix_step1_dm2'|| v_loop_num1 || ' e ';

drop table if exists kp_check_fjs_rows_step11;


tmp8 = 'create table kp_check_fjs_rows_step11
as
select case when zone1>=''0.97'' and zone1<=''1.03'' and zone2>=''0.97'' and zone2<=''1.03''
and zone3>=''0.97'' and zone3<=''1.03'' and zone4>=''0.97'' and zone4<=''1.03''
then true
else false
end as final_op
from (
select zone1,
zone2,
zone3,
zone4
from kp_final_matrix_step1_dm'|| v_loop_num1 || ' b
where source=''fj'') a ' ;

execute tmp1;
--raise notice '%',tmp1;

execute tmp2;
--raise notice '%',tmp1;

execute tmp3;
--raise notice '%',tmp1;

execute tmp4;
--raise notice '%',tmp1;

execute tmp5;
--raise notice '%',tmp1;

execute tmp6;
--raise notice '%',tmp1;
--execute tmp7;
--raise notice '%',tmp1;

execute tmp8;
--raise notice '%',tmp1;

var_gf_row:=(select final_op from kp_check_fjs_rows_step11);


raise notice 'row boolean value is %',var_gf_row;

if var_gf_row then
return;
end if;

tmp9 = 'drop table if exists kp_final_matrix_step2_dm'|| v_loop_num1 || ';';


tmp10= 'create table kp_final_matrix_step2_dm'|| v_loop_num1 || '
as
select a.source,
a.zone1,
a.zone2,
a.zone3,
a.zone4,
round((zone1+zone2+zone3+zone4)) as oi_cur,
oi_hor,
round((oi_hor/(zone1+zone2+zone3+zone4))::numeric(26,3),3) as fi
from (
select a.source,
round(a.zone1*(select zone1 from kp_final_matrix_step1_dm'|| v_loop_num1 || ' c
where source=''fj'')) as zone1,
round(a.zone2*(select zone2 from kp_final_matrix_step1_dm'|| v_loop_num1 || ' d
where source=''fj'')) as zone2,
round(a.zone3*(select zone3 from kp_final_matrix_step1_dm'|| v_loop_num1 || ' e
where source=''fj'')) as zone3,
round(a.zone4*(select zone4 from kp_final_matrix_step1_dm'|| v_loop_num1 || ' f
where source=''fj'')) as zone4,
b.oi_hor
from kp_final_matrix_step1_dm'|| v_loop_num1 || ' a
join kp_pre_final_matrix_dm1 b
on a.source=b.source
where a.source in (''zone1'',''zone2'',''zone3'',''zone4'')) a';

drop table if exists kp_check_fjs_columns_step21;


tmp12='create table kp_check_fjs_columns_step21
as
select case when zone1>=''0.97'' and zone1<=''1.03'' and zone2>=''0.97'' and zone2<=''1.03''
and zone3>=''0.97'' and zone3<=''1.03'' and zone4>=''0.97'' and zone4<=''1.03''
then true
else false
end as final_op
from (
select max(case when source=''zone1'' then fi end ) zone1,
max(case when source=''zone2'' then fi end ) zone2,
max(case when source=''zone3'' then fi end ) zone3,
max(case when source=''zone4'' then fi end ) zone4
from kp_final_matrix_step2_dm'|| v_loop_num1 || ' a )b ';

execute tmp9;
--raise notice '%',tmp9;

execute tmp10;
--raise notice '%',tmp10;

--execute tmp11;
--raise notice '%',tmp11;

execute tmp12;
--raise notice '%',tmp12;

tmp13='drop table if exists kp_pre_final_matrix_dm1'|| v_loop_num1 || ';';


tmp14='create table kp_pre_final_matrix_dm1'|| v_loop_num1 || '
as
select * from kp_pre_final_matrix_dm1 a ';

execute tmp13;
--raise notice '%',tmp13;

execute tmp14;
--raise notice '%',tmp14;

drop table if exists kp_pre_final_matrix_dm1;


tmp15='create table kp_pre_final_matrix_dm1
as
select *
from kp_final_matrix_step2_dm'|| v_loop_num1 || ' a ';

execute tmp15;
--raise notice '%',tmp15;

var_gf_col:=(select final_op from kp_check_fjs_columns_step21);


raise notice 'column boolean value is %',var_gf_col;

if var_gf_col then
v_loop=-1;
end if;

v_loop_num1=v_loop_num1+1;
raise notice '%',v_loop_num1;

end LOOP;

END;
$BODY$
language plpgsql;

commit;
drop table if exists kp_zones_fm;
create table kp_zones_fm
(source varchar,
destination varchar,
distance numeric);

insert into kp_zones_fm values


('zone1','zone2','70'),
('zone1','zone3','90'),
('zone1','zone4','125'),
('zone2','zone1','70'),
('zone2','zone3','75'),
('zone2','zone4','80'),
('zone3','zone1','90'),
('zone3','zone2','75'),
('zone3','zone4','100'),
('zone4','zone1','125'),
('zone4','zone2','80'),
('zone4','zone3','100');

drop table if exists kp_gfsfm0;


create table kp_gfsfm0
(zone varchar,
gfactor numeric(26,3));

insert into kp_gfsfm0 values


('zone1','4.0'),
('zone2','3.0'),
('zone3','5.0'),
('zone4','2.0');
Call string:
begin;
call p_kp_fm_proc();
commit;
OUTPUT
NOTICE: row boolean value is f
NOTICE: column boolean value is f
NOTICE: 1
NOTICE: row boolean value is f
NOTICE: column boolean value is f
NOTICE: 2
NOTICE: row boolean value is t
COMMIT

You might also like