insert into mmt_format_config (f1_modified)values ('Objective Of Test'), ('Tractor Model'), ('Trailer Type'), ('Test Condition_1:'), ('Test Engineer Comments_1'), ('Test Manager Comments_1'), ('Test Condition_2:'), ('Test Engineer Comments_2'), ('Test Manager Comments_2'); 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='haulage_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='haulage') where a.row_number_start is null and format='haulage_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='haulage') where a.row_number_start is null and format='haulage_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='haulage_field_summary'; drop table if exists haulage_field_summary_tractor_model_1; create table haulage_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 ='haulage' and b.format ='haulage_field_summary' order by a.syspk; select * from haulage_field_summary_tractor_model_1; drop table if exists haulage_field_summary_tractor_model_1; create table haulage_field_summary_trailer_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='Trailer Type' and a.src_table_name ='haulage' and b.format ='haulage_field_summary' order by a.syspk; drop table haulage_field_summary_test_condition_1; create table haulage_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 ='haulage' and b.format ='haulage_field_summary' order by a.syspk; drop table haulage_field_summary_test_condition_2; create table haulage_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 ='haulage' and b.format ='haulage_field_summary' order by a.syspk; create table haulage_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 ='haulage' and b.format ='haulage_field_summary' order by a.syspk; create table haulage_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 ='haulage' and b.format ='haulage_field_summary' order by a.syspk; drop table haulage_field_summary_manager_comments_1; create table haulage_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 ='haulage' and b.format ='haulage_field_summary' order by a.syspk; alter table haulage_field_summary_test_condition_1 add column a_c text; alter table haulage_field_summary_test_condition_1 add column transpose_syspk serial primary key; update haulage_field_summary_test_condition_1 set a_c=concat('a_',transpose_syspk); drop table transpose_haulage_field_summary_test_condition_1 create table transpose_haulage_field_summary_test_condition_1 ( dummy text, Test_Condition text, Test_Date text, Tractor_Model text, Fuel_consumption_Ltr_hr text, Mileage_Km_Ltr text, Average_speed_of_travel_kmph text, Total_distance_travelled_km text, Gear_used_on_Straight_Road text, Straight_road_RPM_Drop text, Gear_used_on_Up_Slope_1 text, Up_Slope_RPM_Drop_1 text, Gear_used_on_Up_Slope_2 text, Up_Slope_RPM_Drop_2 text, Gear_used_on_Down_Slope text, Down_Slope_RPM_Shoot_up text, Engine_Smoke_on_Load text, Fuel_consumption_lit_hr_2 text, Mileage_Km_Ltr_2 text, Fuel_consumption_lit_hr_3 text, Mileage_Km_Ltr_3 text, Engine_acceleration_smoke text, Range_Gear_Shifting text, Speed_Gear_Shifting text, Tractor_Steer_ability text, Tractor_braking_performance text, Front_Visibility text, Implement_Accessibility text, Front_end_lifting_during_operation text, RPM_Recovery_Time text, Engine_Vibration text, Engine_Sound text ); insert into transpose_haulage_field_summary_test_condition_1 ( dummy, Test_Condition, Test_Date, Tractor_Model, Fuel_consumption_Ltr_hr, Mileage_Km_Ltr, Average_speed_of_travel_kmph, Total_distance_travelled_km, Gear_used_on_Straight_Road, Straight_road_RPM_Drop, Gear_used_on_Up_Slope_1, Up_Slope_RPM_Drop_1, Gear_used_on_Up_Slope_2, Up_Slope_RPM_Drop_2, Gear_used_on_Down_Slope, Down_Slope_RPM_Shoot_up, Engine_Smoke_on_Load, Fuel_consumption_lit_hr_2, Mileage_Km_Ltr_2, Fuel_consumption_lit_hr_3, Mileage_Km_Ltr_3, Engine_acceleration_smoke, Range_Gear_Shifting, Speed_Gear_Shifting, Tractor_Steer_ability, Tractor_braking_performance, Front_Visibility, Implement_Accessibility, Front_end_lifting_during_operation, RPM_Recovery_Time, Engine_Vibration, Engine_Sound ) 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 haulage_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); alter table haulage_field_summary_test_condition_2 add column a_c text; alter table haulage_field_summary_test_condition_2 add column transpose_syspk serial primary key; update haulage_field_summary_test_condition_2 set a_c=concat('a_',transpose_syspk); create table transpose_haulage_field_summary_test_condition_12 ( dummy text, Test_Condition text, Test_Date text, Tractor_Model text, Fuel_consumption_Ltr_hr text, Mileage_Km_Ltr text, Average_speed_of_travel_kmph text, Total_distance_travelled_km text, Gear_used_on_Straight_Road text, Straight_road_RPM_Drop text, Gear_used_on_Up_Slope_1 text, Up_Slope_RPM_Drop_1 text, Gear_used_on_Up_Slope_2 text, Up_Slope_RPM_Drop_2 text, Gear_used_on_Down_Slope text, Down_Slope_RPM_Shoot_up text, Fuel_consumption_lit_hr_2 text, Mileage_Km_Ltr_2 text, Fuel_consumption_lit_hr_3 text, Mileage_Km_Ltr_3 text, Engine_Smoke_on_Load text, Engine_acceleration_smoke text, Range_Gear_Shifting text, Speed_Gear_Shifting text, Tractor_Steer_ability text, Tractor_braking_performance text, Front_Visibility text, Implement_Accessibility text, Front_end_lifting_during_operation text, RPM_Recovery_Time text, Engine_Vibration text, Engine_Sound text ); insert into transpose_haulage_field_summary_test_condition_12 ( dummy, Test_Condition, Test_Date, Tractor_Model, Fuel_consumption_Ltr_hr, Mileage_Km_Ltr, Average_speed_of_travel_kmph, Total_distance_travelled_km, Gear_used_on_Straight_Road, Straight_road_RPM_Drop, Gear_used_on_Up_Slope_1, Up_Slope_RPM_Drop_1, Gear_used_on_Up_Slope_2, Up_Slope_RPM_Drop_2, Gear_used_on_Down_Slope, Down_Slope_RPM_Shoot_up, Fuel_consumption_lit_hr_2, Mileage_Km_Ltr_2, Fuel_consumption_lit_hr_3, Mileage_Km_Ltr_3, Engine_Smoke_on_Load, Engine_acceleration_smoke, Range_Gear_Shifting, Speed_Gear_Shifting, Tractor_Steer_ability, Tractor_braking_performance, Front_Visibility, Implement_Accessibility, Front_end_lifting_during_operation, RPM_Recovery_Time, Engine_Vibration, Engine_Sound ) 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 haulage_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);