drop function if exists mmt_staging2.fn_FTHLG_SUM_ODS ;
CREATE OR REPLACE FUNCTION mmt_staging2.fn_FTHLG_SUM_ODS()
   RETURNS void AS $$
 begin


execute 'insert into mmt_ods.field_perf_summary_tractor_info 
(
client_id,
function_id,
tractor_model,
tractor_make,
tractor_engine_hp,
rated_rpm,
transmission_type,
wheel_drive_type,
fip_type,
steering_type,
tractor_weight_front_kg,
tractor_weight_rear_kg,
tractor_weight_total_kg,
test_file_ref_no
)
select
client_id,
function_id,
tractor_model,
tractor_make ,
tractor_engine_hp::int,
rated_rpm::int,
transmission_type,
wheel_drive_type,
fip_type,
steering_type,
tractor_weight_kg_front::int  tractor_weight_front_kg,
tractor_weight_kg_rear::int tractor_weight_rear_kg,
tractor_weight_kg_total::int tractor_weight_total_kg,
file_number
from mmt_staging2.fthlg_sum_trac_h1_block where tractor_model<> '0'';

execute 'insert into mmt_ods.field_perf_summary_trailer_info
(
client_id,
function_id,
trailer_type,
tire_size_and_inflation_pressure_psi,
no_of_axle,
no_of_wheels,
trailer_gross_weight_kg,
tractor_rwc_to_hitch_point_center_dist_mm,
tractor_hitch_height_from_ground_mm,
trailer_hitch_height_above_ground_level_mm,
gradient_slope_1_degree,
gradient_slope_2_degree,
tractor_hitch_type,
test_file_ref_no
)
select
client_id,
function_id,
Trailer_Type,
Tire_size_and_inflation_pressure_psi,
No_Of_Axle::int ,
No_Of_Wheels::int,	
Trailer_Gross_Weight_Kg::int,
Tractor_Rear_Wheel_Center_to_Hitch_Point_Center_Distance_mm::int,
Tractor_Hitch_Height_from_Ground_mm::int,
Trailer_hitch_Height_above_ground_level_mm::int,
Gradient_Slope_1_Degree::int,
Gradient_Slope_2_degree::int,
Tractor_Hitch_Type,
file_number
from mmt_staging2.FTHLG_SUM_Trail_Type_Block';

execute 'insert into mmt_ods.field_perf_summary 
(
client_id,
function_id,
test_condition,
test_date,
tractor_model,
fuel_consumption_lit_per_hr,
mileage_km_per_ltr,
avg_speed_of_travel_kmph,
total_dist_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_var_lit_per_hr,
mileage_var_km_per_ltr,
fuel_consumption_var_pct_lit_per_hr,
mileage_var_pct_km_per_ltr,
engine_smoke_on_load,
engine_acceleration_smoke,
range_gear_shifting,
speed_gear_shifting,
tractor_steer_ability,
tractor_braking_perf,
front_visibility,
implement_accessibility,
front_end_lifting_during_operation,
rpm_recovery_time,
engine_vibration,
engine_sound,
test_file_ref_no
)
select
client_id,
function_id,
Test_Condition,
Test_Date::date,
Tractor_Model,
Fuel_consumption_Ltr_hr::float fuel_consumption_lit_per_hr ,
Mileage_Km_Ltr ::float mileage_km_per_ltr ,
Average_speed_of_travel_kmph::float avg_speed_of_travel_kmph ,
Total_distance_travelled_km::int  total_dist_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,
case when fuel_consumption_lit_hr_2 ~ E'^\\d+$' then  fuel_consumption_lit_hr_2::int  else null end as fuel_consumption_var_lit_per_hr ,
case when Mileage_Km_Ltr_2 ~ E'^\\d+$' then  Mileage_Km_Ltr_2::int else null  end as  mileage_var_km_per_ltr,
case when fuel_consumption_lit_hr_3 ~ E'^\\d+$' then  fuel_consumption_lit_hr_3::int  else  null end as fuel_consumption_var_pct_lit_per_hr ,
case when Mileage_Km_Ltr_3 ~ E'^\\d+$' then  Mileage_Km_Ltr_3::int else null end as mileage_var_pct_km_per_ltr,
engine_smoke_on_load,
Engine_acceleration_smoke,
Range_Gear_Shifting,
Speed_Gear_Shifting,
Tractor_Steer_ability,
Tractor_braking_performance tractor_braking_perf ,
Front_Visibility,
Implement_Accessibility,
Front_end_lifting_during_operation,
RPM_Recovery_Time,
Engine_Vibration,
Engine_Sound,
file_number
from mmt_staging2.fthlg_sum_test_condition_1_block where Gear_used_on_Straight_Road not in ('0','0.000')'; 



execute 'insert into mmt_ods.field_perf_summary 
(
client_id,
function_id,
test_condition,
test_date,
tractor_model,
fuel_consumption_lit_per_hr,
mileage_km_per_ltr,
avg_speed_of_travel_kmph,
--total_dist_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_var_lit_per_hr,
mileage_var_km_per_ltr,
fuel_consumption_var_pct_lit_per_hr,
mileage_var_pct_km_per_ltr,
engine_smoke_on_load,
engine_acceleration_smoke,
range_gear_shifting,
speed_gear_shifting,
tractor_steer_ability,
tractor_braking_perf,
front_visibility,
implement_accessibility,
front_end_lifting_during_operation,
rpm_recovery_time,
engine_vibration,
engine_sound,
test_file_ref_no
)
select
client_id,
function_id,
Test_Condition,
Test_Date::date,
Tractor_Model,
Fuel_consumption_Ltr_hr::float fuel_consumption_lit_per_hr ,
Mileage_Km_Ltr ::float mileage_km_per_ltr ,
Average_speed_of_travel_kmph::float avg_speed_of_travel_kmph ,
Total_distance_travelled_km::int end as total_dist_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,
case when fuel_consumption_lit_hr_2 ~ E'^\\d+$' then  fuel_consumption_lit_hr_2::int  else null end as fuel_consumption_var_lit_per_hr ,
case when Mileage_Km_Ltr_2 ~ E'^\\d+$' then  Mileage_Km_Ltr_2::int else null  end as  mileage_var_km_per_ltr,
case when fuel_consumption_lit_hr_3 ~ E'^\\d+$' then  fuel_consumption_lit_hr_3::int  else  null end as fuel_consumption_var_pct_lit_per_hr ,
case when Mileage_Km_Ltr_3 ~ E'^\\d+$' then  Mileage_Km_Ltr_3::int else null end as mileage_var_pct_km_per_ltr,
engine_smoke_on_load,
Engine_acceleration_smoke,
Range_Gear_Shifting,
Speed_Gear_Shifting,
Tractor_Steer_ability,
Tractor_braking_performance tractor_braking_perf ,
Front_Visibility,
Implement_Accessibility,
Front_end_lifting_during_operation,
RPM_Recovery_Time,
Engine_Vibration,
Engine_Sound,
file_number 
from mmt_staging2.fthlg_sum_test_condition_2_block where Gear_used_on_Straight_Road not in ('0','0.000')'; 


end
$$ LANGUAGE plpgsql;

select mmt_staging2.fn_FTHLG_SUM_ODS();
