insert into mmt_ods.mmt_config (file_format,sheet_format,target_table,f1_modified) values ('FTHLG','FTHLG_TRS','FTHLG_TRS_H1','Tractor specifications sheet'), ('FTHLG','FTHLG_TRS','FTHLG_TRS_SPEC','Tractor Specifications'), ('FTHLG','FTHLG_TRS','FTHLG_TRS_Engine_RPM','Engine RPM Data:'), ('FTHLG','FTHLG_TRS','FTHLG_TRS_Trailer','Trailer Details') ; drop table mmt_staging2.mmt_staging_specific_table_deepthi; create table mmt_staging2.mmt_staging_specific_table_deepthi as select * from mmt_staging1.mmt_staging_generic_table where file_format='FTHLG' and sheet_format ='FTHLG_TRS' and file_name='20210217_FTHLG_5. FC_M Star Trem IV Aternate Aapproch_57 HP_HAULAGE_BS3A_SINDHNOOR.xlsx'; alter table mmt_staging2.mmt_staging_specific_table_deepthi add column is_rownumber_fetched int; update mmt_staging2.mmt_staging_specific_table_deepthi set column2 = TRIM (TRAILING FROM column2 ); update mmt_staging2.mmt_staging_specific_table_deepthi set column2 = TRIM (LEADING FROM column2 ); drop table mmt_staging2.mmt_staging_process_table_deepthi ; create table mmt_staging2.mmt_staging_process_table_deepthi (like mmt_staging2.mmt_staging_specific_table_deepthi); alter table mmt_staging2.mmt_staging_process_table_deepthi add column rank int; alter table mmt_staging2.mmt_staging_process_table_deepthi add column rank_tag text; update mmt_ods.mmt_config set F1_modified = TRIM (TRAILING FROM F1_modified) where file_format ='FTHLG' and sheet_format='FTHLG_TRS'; update mmt_ods.mmt_config set F1_modified = TRIM (LEADING FROM F1_modified) where file_format ='FTHLG' and sheet_format='FTHLG_TRS'; update mmt_ods.mmt_config set F1_source=F1_modified where file_format ='FTHLG' and sheet_format='FTHLG_TRS'; update mmt_ods.mmt_config set F1_source = TRIM (TRAILING FROM F1_source) where file_format ='FTHLG' and sheet_format='FTHLG_TRS'; update mmt_ods.mmt_config set F1_source = TRIM (LEADING FROM F1_source) where file_format ='FTHLG' and sheet_format='FTHLG_TRS'; update mmt_ods.mmt_config a set row_number_start=(select min(b.row_number) from mmt_staging2.mmt_staging_specific_table_deepthi b where trim(upper(F1_source))=trim(upper(column2)) and b.is_rownumber_fetched is null) where a.row_number_start is null and a.file_format='FTHLG' and sheet_format='FTHLG_TRS'; update mmt_staging2.mmt_staging_specific_table_deepthi a set is_rownumber_fetched=1 from mmt_ods.mmt_config b where trim(upper(F1_source))=trim(upper(column2)) and b.row_number_start=a.row_number and is_rownumber_fetched is null and b.file_format ='FTHLG' and b.sheet_format='FTHLG_TRS'; update mmt_ods.mmt_config set row_previous_number=row_number_start-1 where file_format ='FTHLG' and sheet_format='FTHLG_TRS'; update mmt_ods.mmt_config a set row_read_end= (select b.row_number_start from mmt_ods.mmt_config b where b.syspk=a.syspk+1 ) where a.file_format='FTHLG' and a.sheet_format='FTHLG_TRS'; /*update mmt_ods.mmt_config set row_read_end = null where f1_modified ='Implement Details' and file_format='FTHLG' and sheet_format='FTHLG_TRS';*/ update mmt_ods.mmt_config a set run_time=current_timestamp where a.file_format='FTHLG' and sheet_format='FTHLG_TRS'; insert into mmt_staging2.mmt_staging_process_table_deepthi select a.* , RANK () OVER ( ORDER BY row_number),'FTHLG_TRS_H1' from mmt_staging2.mmt_staging_specific_table_deepthi a join mmt_ods.mmt_config b on a.row_number >row_number_start and (a.row_number< row_read_end or row_read_end is null) and f1_modified='Tractor specifications sheet' and b.file_format ='FTHLG' and b.sheet_format='FTHLG_TRS' order by a.row_number; insert into mmt_staging2.mmt_staging_process_table_deepthi select a.* , RANK () OVER ( ORDER BY row_number),'FTHLG_TRS_SPEC' from mmt_staging2.mmt_staging_specific_table_deepthi a join mmt_ods.mmt_config b on a.row_number >row_number_start and (a.row_number< row_read_end or row_read_end is null) and f1_modified='Tractor Specifications' and b.file_format ='FTHLG' and b.sheet_format='FTHLG_TRS' order by a.row_number; insert into mmt_staging2.mmt_staging_process_table_deepthi select a.* , RANK () OVER ( ORDER BY row_number),'FTHLG_TRS_Engine_RPM' from mmt_staging2.mmt_staging_specific_table_deepthi a join mmt_ods.mmt_config b on a.row_number >row_number_start and (a.row_number< row_read_end or row_read_end is null) and f1_modified='Engine RPM Data:' and b.file_format ='FTHLG' and b.sheet_format='FTHLG_TRS' order by a.row_number; insert into mmt_staging2.mmt_staging_process_table_deepthi select a.* , RANK () OVER ( ORDER BY row_number),'FTHLG_TRS_Trailer' from mmt_staging2.mmt_staging_specific_table_deepthi a join mmt_ods.mmt_config b on a.row_number >row_number_start and (a.row_number< row_read_end or row_read_end is null) and f1_modified='Trailer Details' and b.file_format ='FTHLG' and b.sheet_format='FTHLG_TRS' order by a.row_number; delete from mmt_staging2.mmt_staging_process_table_deepthi where row_number between 88 and 98; create table mmt_staging2.FTHLG_TRS_H1_INT as select * from mmt_staging2.mmt_staging_process_table_deepthi where rank_tag='FTHLG_TRS_H1'; insert into mmt_staging2.FTHLG_TRS_H1_INT(column2) values ('Date of Test'); insert into mmt_staging2.FTHLG_TRS_H1_INT(column2) values ('Report Date'); insert into mmt_staging2.FTHLG_TRS_H1_INT(column2) values ('Type of Road'); insert into mmt_staging2.FTHLG_TRS_H1_INT (column2) values ('Tractor Hitch Height from Ground, mm'); insert into mmt_staging2.FTHLG_TRS_H1_INT (column2) values ('Tractor Rear Wheel Center to Hitch Point Center Distance, mm'); update mmt_staging2.FTHLG_TRS_H1_INT a set column3=(select column5 from mmt_staging2.FTHLG_TRS_H1_INT b where trim(a.column2)=trim(b.column4) and b.column4='Date of Test') where a.column2='Date of Test'; update mmt_staging2.FTHLG_TRS_H1_INT a set column3=(select column7 from mmt_staging2.FTHLG_TRS_H1_INT b where trim(a.column2)=trim(b.column6) and b.column6='Report Date') where a.column2='Report Date'; update mmt_staging2.FTHLG_TRS_H1_INT a set column3=(select column6 from mmt_staging2.FTHLG_TRS_H1_INT b where trim(a.column2)=trim(b.column5) and b.column5='Type of Road') where a.column2='Type of Road'; update mmt_staging2.FTHLG_TRS_H1_INT a set column3=(select column6 from mmt_staging2.FTHLG_TRS_H1_INT b where trim(a.column2)=trim(b.column5) and b.column5='Tractor Hitch Height from Ground, mm') where a.column2='Tractor Hitch Height from Ground, mm'; update mmt_staging2.FTHLG_TRS_H1_INT a set column3=(select column6 from mmt_staging2.FTHLG_TRS_H1_INT b where trim(a.column2)=trim(b.column5) and b.column5='Tractor Rear Wheel Center to Hitch Point Center Distance, mm') where a.column2='Tractor Rear Wheel Center to Hitch Point Center Distance, mm'; CREATE EXTENSION if not exists tablefunc; alter table mmt_staging2.FTHLG_TRS_H1_INT add column a_c text; update mmt_staging2.FTHLG_TRS_H1_INT set a_c=concat('a_',rank); drop table if exists mmt_staging2.FTHLG_TRS_H1_Block; create table mmt_staging2.FTHLG_TRS_H1_Block ( dummy text, Report_Reference_No text, Objective_Of_Test text, Background_of_Test text, Job_Order_No text, Test_Location text, Gradient_Slope_1_Degree text, Gradient_Slope_2_Degree text, Tractor_Hitch_Type text, Test_Engineer text, Test_Operator text, Date_of_Test text, Report_Date text, Type_of_Road text, Tractor_Hitch_Height_from_Ground_mm text, Tractor_Rear_Wheel_Center_to_Hitch_Point_Center_Distance_mm text ); insert into mmt_staging2.FTHLG_TRS_H1_Block ( dummy, Report_Reference_No, Objective_Of_Test, Background_of_Test, Job_Order_No, Test_Location, Gradient_Slope_1_Degree, Gradient_Slope_2_Degree, Tractor_Hitch_Type, Test_Engineer, Test_Operator, Date_of_Test, Report_Date, Type_of_Road, Tractor_Hitch_Height_from_Ground_mm, Tractor_Rear_Wheel_Center_to_Hitch_Point_Center_Distance_mm ) SELECT * FROM crosstab( 'SELECT unnest(''{column3}''::text[]) AS col , row_number() OVER () , unnest(ARRAY[column3::text]) AS val FROM mmt_staging2.FTHLG_TRS_H1_INT' -- 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); alter table mmt_staging2.FTHLG_TRS_H1_Block add column file_name text; update mmt_staging2.FTHLG_TRS_H1_Block set file_name='20210217_FTHLG_5. FC_M Star Trem IV Aternate Aapproch_57 HP_HAULAGE_BS3A_SINDHNOOR.xslx'; alter table mmt_staging2.FTHLG_TRS_H1_Block add column file_format text; update mmt_staging2.FTHLG_TRS_H1_Block set file_format='FTHLG'; alter table mmt_staging2.FTHLG_TRS_H1_Block add column sheet_format text; update mmt_staging2.FTHLG_TRS_H1_Block set sheet_format='FTHLG_TRS'; alter table mmt_staging2.mmt_staging_process_table_deepthi add column a_c text; update mmt_staging2.mmt_staging_process_table_deepthi set a_c=concat('a_',rank); drop table mmt_staging2.FTHLG_TRS_SPEC_Block; create table mmt_staging2.FTHLG_TRS_SPEC_Block ( dummy text, Tractor_Model text, Tractor_Make text, Tractor_Sr_No text, Tractor_Engine_HP text, FIP_Type text, Hour_Meter_Reading text, Steering_Type text, Transmission_Type text, Wheel_Drive_Type_WD text, EGR_Yes_No text, Brake_Type text, PTO_Type text, Standard_PTO_Speed_RPM text, EPTO_Speed_RPM text, Front_Tyre_Make text, Front_Tyre_Size text, Front_Tyre_Pressure_psi text, Rear_Tyre_Make text, Rear_Tyre_Size text, Rear_Tyre_Pressure_psi text, Tractor_Weight_kg_Front text, Tractor_Weight_kg_Rear text, Tractor_Weight_kg_Total text, Ballasted_Tractor_Accessories text, Mechanical_Ballast_Rear text, Water_Ballast_Rear_75 text, Front_bumper_weight_Kg text, Mechanical_Ballast_Front_in_kg text, Mechanical_Ballast_Front text, Total_Ballast_Weight text ); insert into mmt_staging2.FTHLG_TRS_SPEC_Block ( dummy, Tractor_Model, Tractor_Make, Tractor_Sr_No, Tractor_Engine_HP, FIP_Type, Hour_Meter_Reading, Steering_Type, Transmission_Type, Wheel_Drive_Type_WD, EGR_Yes_No, Brake_Type, PTO_Type, Standard_PTO_Speed_RPM, EPTO_Speed_RPM, Front_Tyre_Make, Front_Tyre_Size, Front_Tyre_Pressure_psi, Rear_Tyre_Make, Rear_Tyre_Size, Rear_Tyre_Pressure_psi, Tractor_Weight_kg_Front, Tractor_Weight_kg_Rear, Tractor_Weight_kg_Total, Ballasted_Tractor_Accessories, Mechanical_Ballast_Rear, Water_Ballast_Rear_75, Front_bumper_weight_Kg , Mechanical_Ballast_Front_in_kg, Mechanical_Ballast_Front , Total_Ballast_Weight ) SELECT * FROM crosstab( 'SELECT unnest(''{column3,column4,column5,column6,column7}''::text[]) AS col , row_number() OVER () , unnest(ARRAY[column3::text,column4::text,column5::text,column6::text,column7::text]) AS val FROM mmt_staging2.mmt_staging_process_table_deepthi where rank_tag=''FTHLG_TRS_SPEC'' 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); alter table mmt_staging2.FTHLG_TRS_SPEC_Block add column file_name text; update mmt_staging2.FTHLG_TRS_SPEC_Block set file_name='20210217_FTHLG_5. FC_M Star Trem IV Aternate Aapproch_57 HP_HAULAGE_BS3A_SINDHNOOR.xlsx'; alter table mmt_staging2.FTHLG_TRS_SPEC_Block add column file_format text; update mmt_staging2.FTHLG_TRS_SPEC_Block set file_format='FTHLG'; alter table mmt_staging2.FTHLG_TRS_SPEC_Block add column sheet_format text; update mmt_staging2.FTHLG_TRS_SPEC_Block set sheet_format='FTHLG_TRS'; create table mmt_staging2.FTHLG_TRS_Engine_RPM_Block ( dummy text, Low_Idle text, High_Idle text, Rated_RPM text, Engine_to_PTO_Ratio_540_PTO text, Engine_to_PTO_Ratio_540E_PTO text ); insert into mmt_staging2.FTHLG_TRS_Engine_RPM_Block ( dummy, Low_Idle, High_Idle, Rated_RPM, Engine_to_PTO_Ratio_540_PTO, Engine_to_PTO_Ratio_540E_PTO ) SELECT * FROM crosstab( 'SELECT unnest(''{column3,column4,column5,column6,column7}''::text[]) AS col , row_number() OVER () , unnest(ARRAY[column1::text,column3::text,column4::text,column5::text,column6::text,column7::text]) AS val FROM mmt_staging2.mmt_staging_process_table_deepthi where rank_tag=''FTHLG_TRS_Engine_RPM'' ORDER BY generate_series(1,6),1' ) t (col text,a_1 text,a_2 text,a_3 text,a_4 text,a_5 text); alter table mmt_staging2.FTHLG_TRS_Engine_RPM_Block add column file_name text; update mmt_staging2.FTHLG_TRS_Engine_RPM_Block set file_name='20210217_FTHLG_5. FC_M Star Trem IV Aternate Aapproch_57 HP_HAULAGE_BS3A_SINDHNOOR.xlsx'; alter table mmt_staging2.FTHLG_TRS_Engine_RPM_Block add column file_format text; update mmt_staging2.FTHLG_TRS_Engine_RPM_Block set file_format='FTHLG'; alter table mmt_staging2.FTHLG_TRS_Engine_RPM_Block add column sheet_format text; update mmt_staging2.FTHLG_TRS_Engine_RPM_Block set sheet_format='FTHLG_TRS'; create table mmt_staging2.FTHLG_TRS_Trailer_Block ( dummy text, Type_Of_Trailer text, No_Of_Axle text, No_Of_Wheels text, Trailer_hitch_Height_above_ground_level_mm text, Make_model_of_trailer text, Trailer_platform_length_mm text, Trailer_platform_Width_mm text, Trailer_platform_Height_mm text, Tire_size text, Inflation_pressure_psi text, Track_width_of_trailer_mm text, Horizontal_distance_of_hitch_point_from_trailer_front_face_mm text, Tractor_Rear_Wheel_Center_to_Hitch_Point_Center_Distance_mm text, Distance_from_Trailerfront_axle_distance_from_hitch_point_mm text, Distance_from_tractor_rear_wheel_center_to_tractor_rear_wheel_center_mm text, Trailer_empty_weight_Kg text, Trailer_Gross_Weight_Kg text, FDPD_TDC_Mar15_009 text ); insert into mmt_staging2.FTHLG_TRS_Trailer_Block ( dummy, Type_Of_Trailer, No_Of_Axle, No_Of_Wheels, Trailer_hitch_Height_above_ground_level_mm, Make_model_of_trailer, Trailer_platform_length_mm, Trailer_platform_Width_mm, Trailer_platform_Height_mm, Tire_size, Inflation_pressure_psi, Track_width_of_trailer_mm, Horizontal_distance_of_hitch_point_from_trailer_front_face_mm, Tractor_Rear_Wheel_Center_to_Hitch_Point_Center_Distance_mm, Distance_from_Trailerfront_axle_distance_from_hitch_point_mm, Distance_from_tractor_rear_wheel_center_to_tractor_rear_wheel_center_mm, Trailer_empty_weight_Kg, Trailer_Gross_Weight_Kg, FDPD_TDC_Mar15_009 ) SELECT * FROM crosstab( 'SELECT unnest(''{column3,column4,column5,column6}''::text[]) AS col , row_number() OVER () , unnest(ARRAY[column3::text,column4::text,column5::text,column6::text]) AS val FROM mmt_staging2.mmt_staging_process_table_deepthi where rank_tag=''FTHLG_TRS_Trailer'' 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); ; alter table mmt_staging2.FTHLG_TRS_Trailer_Block add column file_name text; update mmt_staging2.FTHLG_TRS_Trailer_Block set file_name='20210217_FTHLG_5. FC_M Star Trem IV Aternate Aapproch_57 HP_HAULAGE_BS3A_SINDHNOOR.xlsx'; alter table mmt_staging2.FTHLG_TRS_Trailer_Block add column file_format text; update mmt_staging2.FTHLG_TRS_Trailer_Block set file_format='FTHLG'; alter table mmt_staging2.FTHLG_TRS_Trailer_Block add column sheet_format text; update mmt_staging2.FTHLG_TRS_Trailer_Block set sheet_format='FTHLG_TRS';