
drop function if exists mmt_staging2.fn_FTWET_SUM_ODS ;
CREATE OR REPLACE FUNCTION mmt_staging2.fn_FTWET_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.ftwet_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.ftwet_sum_implement_block';

execute 'insert into mmt_ods.field_perf_summary_implement_info 
(
client_id,
function_id,
type_of_cage_wheel,
cage_wheel_width_mm,
cage_wheel_weight,
cage_wheel_inner_and_outer_ring_dia_mm,
cage_wheel_center_ring_dia_mm,
dist_between_rhs_and_lhs_cage_wheel_inner_ring_mm,
no_of_angles_on_cage_wheel,
test_file_ref_no
)
select
client_id,
function_id,
Type_of_Cage_Wheel,
Cage_Wheel_Width_mm::int,
Cage_wheel_Weight::int,
Cage_Wheel_inner_Outer_Ring_Dia_mm::int cage_wheel_inner_and_outer_ring_dia_mm ,
Cage_Wheel_Center_Ring_Dia_mm::int,
Distance_between_RHS_LHS_cage_wheel_Inner_Ring_mm::int dist_between_rhs_and_lhs_cage_wheel_inner_ring_mm ,
No_of_angles_on_cage_wheel::int,
file_number
from mmt_staging2.FTWET_SUM_Cage_Wheel_Block';


execute 'insert into mmt_ods.field_perf_summary
(
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,
depth_of_cut_cm,
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_perf,
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_Quality,
Pulverization_Index,
test_file_ref_no
)
select
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_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::int,
Avg_Depth_of_cut_cm depth_of_cut_cm ,
Fuel_consumption_lit_hr::float fuel_consumption_lit_per_hr,
Area_covered_acr_hr::float 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 tractor_braking_perf,
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_Quality,
Pulverization_Index,
file_number
from mmt_staging2.FTWET_SUM_Test_Condition_Block where engine_rpm_set <> '0'';

end
$$ LANGUAGE plpgsql;

select mmt_staging2.fn_FTWET_SUM_ODS();
