insert into mmt_format_config (f1_modified) values ('Tractor Model'), ('Make of Implement '), ('Test Condition:_1'), ('Test Engineer Comments_1'), ('Test Manager Comments_1'), ('Test Condition:_2'), ('Test Engineer Comments_2'), ('Test Manager Comments_2'); select * from mmt_format_config; update mmt_format_config set f1_source=f1_modified where f1_source is null; update mmt_format_config set f1_source=trim(trailing from f1_source); update mmt_format_config set f1_source=trim(leading from f1_source); update mmt_format_config set format='plough_field_summary' where format is null; update mmt_format_config set f1_source = replace(f1_source,'_1',''); update mmt_format_config a set row_number_start=(select min(b.syspk) from union_field_summary b where f1_source=column1 and b.is_rownumber_fetched is NULL and b.src_table_name='plough') where a.row_number_start is null and format='plough_field_summary'; update union_field_summary a set is_rownumber_fetched=1 from mmt_format_config b where F1_source=column1 and b.row_number_start=a.syspk and is_rownumber_fetched is null; update mmt_format_config set f1_source = replace(f1_source,'_2',''); update mmt_format_config a set row_number_start=(select min(b.syspk) from union_field_summary b where f1_source=column1 and b.is_rownumber_fetched is NULL and b.src_table_name='plough') where a.row_number_start is null and format='plough_field_summary'; update union_field_summary a set is_rownumber_fetched=1 from mmt_format_config b where F1_source=column1 and b.row_number_start=a.syspk and is_rownumber_fetched is null; update mmt_format_config set row_previous_number=row_number_start-1 where row_previous_number is null; update mmt_format_config a set row_read_end= (select b.row_number_start from mmt_format_config b where b.syspk=a.syspk+1) where a.format='plough_field_summary'; drop table if exists dry_field_summary_tractor_model_1; create table dry_field_summary_tractor_model_1 as select a.* from union_field_summary a join mmt_format_config b on a.syspk>=row_number_start and (a.syspk< row_read_end or row_read_end is null) and f1_modified='Tractor Model' and a.src_table_name ='plough' and b.format ='plough_field_summary' order by a.syspk; select * from dry_field_summary_tractor_model_1; drop table if exists dry_field_summary_make_implement_2; create table dry_field_summary_make_implement_2 as select a.* from union_field_summary a join mmt_format_config b on a.syspk>=row_number_start and (a.syspk< row_read_end or row_read_end is null) and f1_modified='Make of Implement ' and a.src_table_name ='plough' and b.format ='plough_field_summary' order by a.syspk; select * from dry_field_summary_make_implement_2; drop table if exists dry_field_summary_test_condition_1; create table dry_field_summary_test_condition_1 as select a.* from union_field_summary a join mmt_format_config b on a.syspk>=row_number_start and (a.syspk< row_read_end or row_read_end is null) and f1_modified='Test Condition:_1' and a.src_table_name ='plough' and b.format ='plough_field_summary' order by a.syspk; drop table if exists dry_field_summary_engineer_comments_1 ; create table dry_field_summary_engineer_comments_1 as select a.* from union_field_summary a join mmt_format_config b on a.syspk>=row_number_start and (a.syspk< row_read_end or row_read_end is null) and f1_modified='Test Engineer Comments_1' and a.src_table_name ='plough' and b.format ='plough_field_summary' order by a.syspk; drop table if exists dry_field_summary_manager_comments_1 ; create table dry_field_summary_manager_comments_1 as select a.* from union_field_summary a join mmt_format_config b on a.syspk>=row_number_start and (a.syspk< row_read_end or row_read_end is null) and f1_modified='Test Manager Comments_1' and a.src_table_name ='plough' and b.format ='plough_field_summary' order by a.syspk; drop table if exists dry_field_summary_test_condition_2; create table dry_field_summary_test_condition_2 as select a.* from union_field_summary a join mmt_format_config b on a.syspk>=row_number_start and (a.syspk< row_read_end or row_read_end is null) and f1_modified='Test Condition:_2' and a.src_table_name ='plough' and b.format ='plough_field_summary' order by a.syspk; drop table if exists dry_field_summary_engineer_comments_2 ; create table dry_field_summary_engineer_comments_2 as select a.* from union_field_summary a join mmt_format_config b on a.syspk>=row_number_start and (a.syspk< row_read_end or row_read_end is null) and f1_modified='Test Engineer Comments_2' and a.src_table_name ='plough' and b.format ='plough_field_summary' order by a.syspk; drop table if exists dry_field_summary_manager_comments_2 ; select * from dry_field_summary_manager_comments_2; create table dry_field_summary_manager_comments_2 as select a.* from union_field_summary a join mmt_format_config b on a.syspk>=row_number_start and (a.syspk< row_read_end or row_read_end is null) and f1_modified='Test Manager Comments_2' and a.src_table_name ='plough' and b.format ='plough_field_summary' order by a.syspk; alter table dry_field_summary_test_condition_1 add column a_c text; alter table dry_field_summary_test_condition_1 add column transpose_syspk serial primary key; update dry_field_summary_test_condition_1 set a_c=concat('a_',transpose_syspk); drop table transpose_dry_field_summary_test_condition_1; create table transpose_dry_field_summary_test_condition_1 ( dummy text, Test_Condition text, Test_Date text, Tractor_Model text, Engine_RPM_set text, PTO_RPM_set text, Gear_Used text, Nominal_Speed_KMPH text, Engine_RPM_Drop_on_straight text, Engine_RPM_Drop_on_turn text, Depth_of_cut_cm text, No_load_speed_kmph text, On_load_speed_kmph text, wheel_slippage_ text, Fuel_consumption_lit_hr text, Area_covered_acr_hr text, Fuel_consumption_lit_Acr text, M_M_Performance_in_compared_to_respective_competitor_tractors text, Fuel_consumption_lit_hr_2 text, Area_covered_acr_hr_2 text, Fuel_consumption_lit_Acr_2 text, Trail_Observations text, Engine_Smoke_on_Load text, Engine_acceleration_smoke text, Draft_Response text, Tractor_Steer_ability text, Tractor_braking_performance text, Front_Visibility text, Implement_Accessibility text, Front_Wheel_dragging_at_turning text, Front_end_lifting_during_operation text, RPM_Recovery_Time text, Engine_Vibration text, Engine_Sound text, Implement_Lifting_Lowering_response text, Pulverization_Quality text, Pulverization_Index text ); insert into transpose_dry_field_summary_test_condition_1 ( dummy, Test_Condition, Test_Date, Tractor_Model, Engine_RPM_set, PTO_RPM_set, Gear_Used, Nominal_Speed_KMPH, Engine_RPM_Drop_on_straight, Engine_RPM_Drop_on_turn, Depth_of_cut_cm, No_load_speed_kmph, On_load_speed_kmph, wheel_slippage_, Fuel_consumption_lit_hr, Area_covered_acr_hr, Fuel_consumption_lit_Acr, M_M_Performance_in_compared_to_respective_competitor_tractors, Fuel_consumption_lit_hr_2, Area_covered_acr_hr_2, Fuel_consumption_lit_Acr_2, Trail_Observations, Engine_Smoke_on_Load, Engine_acceleration_smoke, Draft_Response, Tractor_Steer_ability, Tractor_braking_performance, Front_Visibility, Implement_Accessibility, Front_Wheel_dragging_at_turning, Front_end_lifting_during_operation, RPM_Recovery_Time, Engine_Vibration, Engine_Sound, Implement_Lifting_Lowering_response, Pulverization_Quality, Pulverization_Index ) SELECT * FROM crosstab( 'SELECT unnest(''{syspk,src_table_name,column1,column2,column3,column4,column5,column6,column7,column8,column9,column10,column11,column12,is_rownumber_fetched}''::text[]) AS col , row_number() OVER () , unnest(ARRAY[syspk::text,src_table_name::text,column1::text,column2::text,column3::text, column4::text,column5::text,column6::text,column7::text,column8::text,column8::text, column9::text,column10::text,column11::text,column12::text,is_rownumber_fetched::text]) AS val FROM dry_field_summary_test_condition_1 ORDER BY generate_series(1,15),2' ) t (col text,a_1 text,a_2 text,a_3 text,a_4 text,a_5 text,a_6 text,a_7 text,a_8 text,a_9 text,a_10 text, a_11 text,a_12 text,a_13 text,a_14 text,a_15 text,a_16 text,a_17 text,a_18 text,a_19 text,a_20 text ,a_21 text,a_22 text,a_23 text,a_24 text,a_25 text,a_26 text,a_27 text,a_28 text,a_29 text,a_30 text, a_31 text,a_32 text,a_33 text,a_34 text,a_35 text,a_36 text); alter table dry_field_summary_test_condition_2 add column a_c text; alter table dry_field_summary_test_condition_2 add column transpose_syspk serial primary key; update dry_field_summary_test_condition_2 set a_c=concat('a_',transpose_syspk); drop table transpose_dry_field_summary_test_condition_1; create table transpose_dry_field_summary_test_condition_2 ( dummy text, Test_Condition text, Test_Date text, Tractor_Model text, Engine_RPM_set text, PTO_RPM_set text, Gear_Used text, Nominal_Speed_KMPH text, Engine_RPM_Drop_on_straight text, Engine_RPM_Drop_on_turn text, Depth_of_cut_cm text, No_load_speed_kmph text, On_load_speed_kmph text, wheel_slippage_ text, Fuel_consumption_lit_hr text, Area_covered_acr_hr text, Fuel_consumption_lit_Acr text, M_M_Performance_in_compared_to_respective_competitor_tractors text, Fuel_consumption_lit_hr_2 text, Area_covered_acr_hr_2 text, Fuel_consumption_lit_Acr_2 text, Trail_Observations text, Engine_Smoke_on_Load text, Engine_acceleration_smoke text, Draft_Response text, Tractor_Steer_ability text, Tractor_braking_performance text, Front_Visibility text, Implement_Accessibility text, Front_Wheel_dragging_at_turning text, Front_end_lifting_during_operation text, RPM_Recovery_Time text, Engine_Vibration text, Engine_Sound text, Implement_Lifting_Lowering_response text, Pulverization_Quality text, Pulverization_Index text ); insert into transpose_dry_field_summary_test_condition_2 ( dummy, Test_Condition, Test_Date, Tractor_Model, Engine_RPM_set, PTO_RPM_set, Gear_Used, Nominal_Speed_KMPH, Engine_RPM_Drop_on_straight, Engine_RPM_Drop_on_turn, Depth_of_cut_cm, No_load_speed_kmph, On_load_speed_kmph, wheel_slippage_, Fuel_consumption_lit_hr, Area_covered_acr_hr, Fuel_consumption_lit_Acr, M_M_Performance_in_compared_to_respective_competitor_tractors, Fuel_consumption_lit_hr_2, Area_covered_acr_hr_2, Fuel_consumption_lit_Acr_2, Trail_Observations, Engine_Smoke_on_Load, Engine_acceleration_smoke, Draft_Response, Tractor_Steer_ability, Tractor_braking_performance, Front_Visibility, Implement_Accessibility, Front_Wheel_dragging_at_turning, Front_end_lifting_during_operation, RPM_Recovery_Time, Engine_Vibration, Engine_Sound, Implement_Lifting_Lowering_response, Pulverization_Quality, Pulverization_Index ) SELECT * FROM crosstab( 'SELECT unnest(''{syspk,src_table_name,column1,column2,column3,column4,column5,column6,column7,column8,column9,column10,column11,column12,is_rownumber_fetched}''::text[]) AS col , row_number() OVER () , unnest(ARRAY[syspk::text,src_table_name::text,column1::text,column2::text,column3::text, column4::text,column5::text,column6::text,column7::text,column8::text,column8::text, column9::text,column10::text,column11::text,column12::text,is_rownumber_fetched::text]) AS val FROM dry_field_summary_test_condition_2 ORDER BY generate_series(1,15),2' ) t (col text,a_1 text,a_2 text,a_3 text,a_4 text,a_5 text,a_6 text,a_7 text,a_8 text,a_9 text,a_10 text, a_11 text,a_12 text,a_13 text,a_14 text,a_15 text,a_16 text,a_17 text,a_18 text,a_19 text,a_20 text ,a_21 text,a_22 text,a_23 text,a_24 text,a_25 text,a_26 text,a_27 text,a_28 text,a_29 text,a_30 text, a_31 text,a_32 text,a_33 text,a_34 text,a_35 text,a_36 text);