insert into mmt_format_config (f1_modified) values ('Tractor Model'), ('Make of Implement '), ('Type of Cage Wheel'), ('Test Condition:'), ('Test Manager Comments'); 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='wet_field_summary' where format is null; 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='rotavator') where a.row_number_start is null and format='wet_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='wet_field_summary'; drop table if exists wet_field_summary_tractor_model_1; create table wet_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 ='rotavator' and b.format ='wet_field_summary' order by a.syspk; drop table if exists wet_field_summary_make_implement_2; create table wet_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 ='rotavator' and b.format ='wet_field_summary' order by a.syspk; drop table if exists wet_field_summary_test_condition; create table wet_field_summary_type_cage_wheel 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='Type of Cage Wheel' and a.src_table_name ='rotavator' and b.format ='wet_field_summary' order by a.syspk; drop table if exists wet_field_summary_test_condition; create table wet_field_summary_test_condition 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:' and a.src_table_name ='rotavator' and b.format ='wet_field_summary' order by a.syspk; drop table wet_field_summary_manager_comments; create table wet_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' and a.src_table_name ='rotavator' and b.format ='wet_field_summary' order by a.syspk; alter table wet_field_summary_test_condition add column a_c text; alter table wet_field_summary_test_condition add column transpose_syspk serial primary key; update wet_field_summary_test_condition set a_c=concat('a_',transpose_syspk); select * from wet_field_summary_test_condition; drop table transpose_wet_field_summary_test_condition; create table transpose_wet_field_summary_test_condition ( 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_1st_Pass text, Engine_RPM_Drop_on_straight_2nd_Pass text, Engine_RPM_Drop_on_straight_3rd_Pass text, Engine_RPM_Drop_on_turn_1st_Pass text, Engine_RPM_Drop_on_turn_2nd_Pass text, Engine_RPM_Drop_on_turn_3rd_Pass text, No_of_passes text, Avg_Depth_of_cut_cm text, Fuel_consumption_lit_hr text, Area_covered_acr_hr text, Fuel_consumption_lit_Acr 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, ); select * from transpose_wet_field_summary_test_condition; insert into transpose_wet_field_summary_test_condition ( dummy, Test_Condition, Test_Date, Tractor_Model, Engine_RPM_set, PTO_RPM_set, Gear_Used, Nominal_Speed_KMPH, Engine_RPM_Drop_on_straight_1st_Pass, Engine_RPM_Drop_on_straight_2nd_Pass, Engine_RPM_Drop_on_straight_3rd_Pass, Engine_RPM_Drop_on_turn_1st_Pass, Engine_RPM_Drop_on_turn_2nd_Pass, Engine_RPM_Drop_on_turn_3rd_Pass, No_of_passes, Avg_Depth_of_cut_cm, Fuel_consumption_lit_hr, Area_covered_acr_hr, Fuel_consumption_lit_Acr, 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 wet_field_summary_test_condition 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,a_37 text);