drop function if exists mmt_staging2.fn_FTDRY_SUM_ODS ;
CREATE OR REPLACE FUNCTION mmt_staging2.fn_FTDRY_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::float,
rated_rpm::int,
transmission_type,
wheel_drive_type,
fip_type,
steering_type,
tractor_weight_kg_front::int,
tractor_weight_kg_rear::int,
tractor_weight_kg_total::int,
file_number
from mmt_staging2.ftdry_sum_trac_h1_block where  tractor_model<> '0'';



execute 'insert into mmt_ods.field_perf_summary_implement_info 
(
client_id,
function_id,
make_of_implement,
implement_type,
implement_size,
hitch_category,
implement_weight,
soil_type,
soil_moisture_content_pct,
soil_bulk_density_g_per_cc,
soil_cone_index,
field_condition,
season,
test_file_ref_no
)
select
client_id,
function_id,
make_of_implement,
implement_type,
implement_size,
hitch_category,
implement_weight::int,
soil_type,
moisture_content::int soil_moisture_content_pct,
bulk_density_g_cc::int soil_bulk_density_g_per_cc,
soil_cone_index::int soil_cone_index,
field_condition,
season,
file_number
from mmt_staging2.ftdry_sum_implement_block';


execute 'insert into mmt_ods.field_perf_summary 
(
client_id,
function_id,
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_turn_1st_pass,
depth_of_cut_cm,
no_load_speed_kmph,
on_load_speed_kmph,
wheel_slippage_pct,
fuel_consumption_lit_per_hr,
area_covered_acr_per_hr,
fuel_consumption_lit_per_acr,
fuel_consumption_var_lit_per_hr,
area_covered_var_acr_per_hr,
fuel_consumption_var_lit_per_acr,
engine_smoke_on_load,
engine_acceleration_smoke,
draft_response,
tractor_steer_ability,
tractor_braking_perfprmance,
front_visibility,
implement_accessibility,
front_wheel_dragging_at_turning,
front_end_lifting_during_operation,
rpm_recovery_time,
engine_vibration,
engine_sound,
implement_lifting_per_lowering_response,
pulverization_index,
pulverization_quality,
test_file_ref_no 
)
select
client_id,
function_id,
test_condition,
test_date::date,
tractor_model,
engine_rpm_set::int,
pto_rpm_set::int,
gear_used,
nominal_speed_kmph::float,
engine_rpm_drop_on_straight engine_rpm_drop_on_straight_1st_pass ,
engine_rpm_drop_on_turn engine_rpm_drop_on_turn_1st_pass,
depth_of_cut_cm,
no_load_speed_kmph::int,
on_load_speed_kmph::int,
wheel_slippage_::int wheel_slippage_pct,
fuel_consumption_lit_hr::float fuel_consumption_lit_per_hr,
area_covered_acr_hr::int area_covered_acr_per_hr,
fuel_consumption_lit_acr::float fuel_consumption_lit_per_acr ,
fuel_consumption_lit_hr_2::int fuel_consumption_var_lit_per_hr,
area_covered_acr_hr_2::int area_covered_var_acr_per_hr ,
fuel_consumption_lit_acr_2::int  fuel_consumption_var_lit_per_acr,
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 implement_lifting_per_lowering_response,
pulverization_index,
pulverization_quality,
file_number
from mmt_staging2.ftdry_sum_test_condition_1_block where engine_rpm_set <> '0'';

execute 'insert into mmt_ods.field_perf_summary 
(
client_id,
function_id,
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_turn_1st_pass,
depth_of_cut_cm,
no_load_speed_kmph,
on_load_speed_kmph,
wheel_slippage_pct,
fuel_consumption_lit_per_hr,
area_covered_acr_per_hr,
fuel_consumption_lit_per_acr,
fuel_consumption_var_lit_per_hr,
area_covered_var_acr_per_hr,
fuel_consumption_var_lit_per_acr,
engine_smoke_on_load,
engine_acceleration_smoke,
draft_response,
tractor_steer_ability,
tractor_braking_perfprmance,
front_visibility,
implement_accessibility,
front_wheel_dragging_at_turning,
front_end_lifting_during_operation,
rpm_recovery_time,
engine_vibration,
engine_sound,
implement_lifting_per_lowering_response,
pulverization_index,
pulverization_quality,
test_file_ref_no 
)
select
client_id,
function_id,
test_condition,
test_date::date,
tractor_model,
engine_rpm_set::int,
pto_rpm_set::int,
gear_used,
nominal_speed_kmph::float,
engine_rpm_drop_on_straight engine_rpm_drop_on_straight_1st_pass ,
engine_rpm_drop_on_turn engine_rpm_drop_on_turn_1st_pass,
depth_of_cut_cm,
no_load_speed_kmph::int,
on_load_speed_kmph::int,
wheel_slippage_::int wheel_slippage_pct,
fuel_consumption_lit_hr::float fuel_consumption_lit_per_hr,
area_covered_acr_hr::int area_covered_acr_per_hr,
fuel_consumption_lit_acr::float fuel_consumption_lit_per_acr ,
fuel_consumption_lit_hr_2::int fuel_consumption_var_lit_per_hr,
area_covered_acr_hr_2::int area_covered_var_acr_per_hr ,
fuel_consumption_lit_acr_2::int  fuel_consumption_var_lit_per_acr,
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 implement_lifting_per_lowering_response,
pulverization_index,
pulverization_quality
file_number,
from mmt_staging2.ftdry_sum_test_condition_2_block where engine_rpm_set <> '0'';

end
$$ LANGUAGE plpgsql;

select mmt_staging2.fn_FTDRY_SUM_ODS();

