insert into mmt_ods.mmt_config
(file_format,sheet_name,target_table,f1_modified)
values
('FTDRY','tractor_specifications','FTDRY_tractor_specifications_Tractor Specifications Sheet','Tractor Specifications Sheet'),       
('FTDRY','tractor_specifications','FTDRY_tractor_specifications_Tractor Specifications','Tractor Specifications'),
('FTDRY','tractor_specifications','FTDRY_tractor_specifications_Engine RPM Data:','Engine RPM Data:'),
('FTDRY','tractor_specifications','FTDRY_tractor_specifications_Implement  Details','Implement  Details')
;

drop table if exists mmt_staging2.process_table_deepthi;

create table mmt_staging2.process_table_deepthi as
select * from mmt_staging1.mmt_staging_generic_table
where file_format='FTDRY' and sheet_name='Tractor specifications'
and file_name
='20210217_FTDRY_1. FC - Arjun 555 HBTU Plough_dry land L3 @ 1500 with Arjun 605 Sindhnoor.xlsx';

alter table mmt_staging2.process_table_deepthi add column is_rownumber_fetched int;

select * from mmt_staging2.process_table_deepthi;

update mmt_staging2.process_table_deepthi set column2 =  TRIM (TRAILING FROM column2 );
update mmt_staging2.process_table_deepthi set column2 =  TRIM (LEADING FROM column2 );

update mmt_ods.mmt_config set F1_modified =  TRIM (TRAILING FROM F1_modified);
update mmt_ods.mmt_config set F1_modified =  TRIM (LEADING FROM F1_modified);


update mmt_ods.mmt_config set F1_source=F1_modified where f1_source is null;

update mmt_ods.mmt_config set F1_source =  TRIM (TRAILING FROM F1_source);
update mmt_ods.mmt_config set F1_source =  TRIM (LEADING FROM F1_source);

update mmt_ods.mmt_config a  
set row_number_start=(select min(b.row_number)
from mmt_staging2.process_table_deepthi  b
where F1_source=column2
and b.is_rownumber_fetched is null)
where a.row_number_start is null and a.file_format='FTDRY';

update mmt_staging2.process_table_deepthi  a  
set is_rownumber_fetched=1
from mmt_ods.mmt_config  b
where F1_source=column2
and b.row_number_start=a.row_number
and is_rownumber_fetched is null;

update mmt_ods.mmt_config  set row_previous_number=row_number_start-1 where row_previous_number is null; 

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='FTDRY';

drop table if exists mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet;

create table mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet
as
select column2 c1,column3 c2,column4 c3,column5 c4,column6 c5,column7 c6
from  mmt_staging2.process_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 ='FTDRY'
order by a.row_number;

select * from mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet;

drop table if exists mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications;

create table mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications
as
select column2 c1,column3 c2,column4 c3 from  mmt_staging2.process_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 ='FTDRY'
order by a.row_number;

select * from mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications;

drop table if exists mmt_staging1.FTDRY_tractor_specifications_Engine_RPM_Data;

create table mmt_staging1.FTDRY_tractor_specifications_Engine_RPM_Data
as
select column2 c1,column3 c2,column4 c3 from  mmt_staging2.process_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 ='FTDRY'
order by a.row_number;

select * from  mmt_staging1.FTDRY_tractor_specifications_Engine_RPM_Data;

drop table if exists mmt_staging1.FTDRY_tractor_specifications_Implement_Details;

create table mmt_staging1.FTDRY_tractor_specifications_Implement_Details
as
select column2 c1,column3 c2,column4 c3,column5 c4,column6 c5 from  mmt_staging2.process_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  a.column2 <> ''
and b.f1_modified='Implement  Details' and b.file_format ='FTDRY'
order by a.row_number;

select * from mmt_staging1.FTDRY_tractor_specifications_Implement_Details;

insert into mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet
(c1) values ('Date of Test');

insert into mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet
(c1) values ('Report Date');

insert into mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet
(c1) values ('Season');

insert into mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet(c1) 
values ('Type of Soil');

insert into mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet(c1) 
values ('Soil Cone Index (kPa)');

insert into mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet
(c1) values ('Field Condition');

update mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet a
set c2=(select c4 from mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet b
where trim(a.c1)=trim(b.c3)
and b.c3='Date of Test')
where  a.c1='Date of Test' ;

update mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet a
set c2=(select c6 from mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet b
where trim(a.c1)=trim(b.c5)
and b.c5='Report Date')
where  a.c1='Report Date';

update mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet a
set c2=(select c5 from mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet b
where trim(a.c1)=trim(b.c4)
and b.c4='Season')
where  a.c1='Season';

update mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet a
set c2=(select c5 from mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet b
where trim(a.c1)=trim(b.c4)
and b.c4='Type of Soil')
where  a.c1='Type of Soil';


update mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet a
set c2=(select c5 from mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet b
where trim(a.c1)=trim(b.c4)
and b.c4='Soil Cone Index (kPa)')
where  a.c1='Soil Cone Index (kPa)';


update mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet a
set c2=(select c5 from mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet b
where trim(a.c1)=trim(b.c4)
and b.c4='Field Condition')
where  a.c1='Field Condition';

CREATE EXTENSION tablefunc;


alter table  mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet add column a_c text;

alter table  mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet add column transpose_rownumber  serial primary key;

update  mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet set a_c=concat('a_',transpose_rownumber);

select * from  mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet;

drop table if exists mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet_tr;

create table mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet_tr
(
dummy text,
Report_Reference_No text,
Objective_Of_Test text,
Background_of_Test text,
Job_Order_No text,       
Test_Location text,
Soil_Moisture_Content_ text,
Soil_Bulk_Density_g_cc  text,
Test_Engineer text,
Test_Operator text,
Date_of_Test text,
Report_Date text,
Season text,
Type_of_Soil text,
Soil_Cone_Index_kPa text,
Field_Condition text
);

insert into  mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet_tr
(
dummy,
Report_Reference_No,
Objective_Of_Test,
Background_of_Test,
Job_Order_No,    
Test_Location,
Soil_Moisture_Content_,
Soil_Bulk_Density_g_cc,
Test_Engineer,
Test_Operator,
Date_of_Test,
Report_Date,
Season,
Type_of_Soil,
Soil_Cone_Index_kPa,
Field_Condition
)
SELECT *
FROM   crosstab(
       'SELECT  unnest(''{c1,c2,c3,c4,c5,c6}''::text[]) AS col
             , row_number() OVER ()
             , unnest(ARRAY[c1::text,c2::text,c3::text,c4::text,c5::text,c6::text]) AS val
        FROM  mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_Sheet
        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_staging1.FTDRY_tractor_specifications_Tractor_Specifications drop column  if exists a_c ;
 
alter table  mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications add column a_c text;

alter table  mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications add column transpose_rownumber  serial primary key;

select * from  mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications;

update mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications set a_c=concat('a_',transpose_rownumber);

drop table mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_tr;

create table mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_tr
(
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,
Mechanical_Ballast_Rear_in_KG text,
Water_Ballast_Rear text,
Mechanical_Ballast_Front text,
Mechanical_Ballast_Front_in_Kg text,
Total_Ballast_Weight text
);

insert into mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications_tr
(
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,
Mechanical_Ballast_Rear_in_KG ,
Water_Ballast_Rear,
Mechanical_Ballast_Front,
Mechanical_Ballast_Front_in_Kg,
Total_Ballast_Weight
)
SELECT *
FROM   crosstab(
       'SELECT  unnest(''{c1,c2,c3}''::text[]) AS col
             , row_number() OVER ()
             , unnest(ARRAY[c1::text,c2::text,c3::text]) AS val
        FROM  mmt_staging1.FTDRY_tractor_specifications_Tractor_Specifications
        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_staging1.FTDRY_tractor_specifications_Engine_RPM_Data  add column a_c text;

alter table   mmt_staging1.FTDRY_tractor_specifications_Engine_RPM_Data add column transpose_rownumber  serial primary key;

update  mmt_staging1.FTDRY_tractor_specifications_Engine_RPM_Data set a_c=concat('a_',transpose_rownumber);
 

create  table  mmt_staging1.FTDRY_tractor_specifications_Engine_RPM_Data_tr
(
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_staging1.FTDRY_tractor_specifications_Engine_RPM_Data_tr
(
dummy,
Low_Idle,
High_Idle,
Rated_RPM,
Engine_to_PTO_Ratio_540_PTO,
Engine_to_PTO_Ratio_540E_PTO
)
SELECT *
FROM   crosstab(
       'SELECT  unnest(''{c1,c2,c3}''::text[]) AS col
             , row_number() OVER ()
             , unnest(ARRAY[c1::text,c2::text,c3::text]) AS val
        FROM   mmt_staging1.FTDRY_tractor_specifications_Engine_RPM_Data
        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);


 alter table  ftdry_tractor_specifications_implement_details add column a_c text;


alter table  ftdry_tractor_specifications_implement_details add column transpose_rownumber  serial primary key;

update ftdry_tractor_specifications_implement_details  set a_c=concat('a_',transpose_rownumber);
 

drop table ftdry_tractor_specifications_implement_details_tr
 
create table  ftdry_tractor_specifications_implement_details_tr
(
dummy text,
Name_of_Implement text,
Type_of_Implement text,
Make_of_Implement text,
No_of_bottoms_Tyne_Disc_Blade text,
Cutting_Width_m text,
Implement_Weight_Kg text,
Hitch_Category text,
Span_cm_For_Mounted_Implement text,
Mast_Height_cm_For_Mounted_Implement text,
FDPD_TDC_Mar15_008 text
);


insert into ftdry_tractor_specifications_implement_details_tr
(
dummy,
Name_of_Implement,
Type_of_Implement,
Make_of_Implement,
No_of_bottoms_Tyne_Disc_Blade, 
Cutting_Width_m,
Implement_Weight_Kg,
Hitch_Category,
Span_cm_For_Mounted_Implement,
Mast_Height_cm_For_Mounted_Implement,
FDPD_TDC_Mar15_008
)
SELECT *
FROM   crosstab(
       'SELECT  unnest(''{c1,c2,c3,c4,c5}''::text[]) AS col
             , row_number() OVER ()
             , unnest(ARRAY[c1::text,c2::text,c3::text,c4::text,c5::text]) AS val
        FROM  ftdry_tractor_specifications_implement_details
        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);
;   


