insert into {p_mydt10tbl2}
( tbl, tbl_nr01, ptbl, ptbl_nr01, comments, mydt57_p00, mydt58_p00, 
  vatcat, vatexcat, wthhldpcat, otaxpcat, stamppcat,
  feestype, invdtype, mydt61_p00, mydt62_p00, netvalue, fpavalue, wthhld_am, otax_am, fees_am, stampvalue)


SELECT tbl01.tbl, 
       tbl01.tbl_nr01, 
       tbl01.ptbl, 
       tbl01.ptbl_nr01, 
       tbl01.comments, 
       if(tbl01.mydt10p00=1, tbl01.mydt57_p00, '') as mydt57_p00,
       if(tbl01.mydt10p00=2, tbl01.mydt58_p00, '') as mydt58_p00,
       tbl01.vatcat,
       tbl01.vatexcat,       
       tbl01.wthhldpcat,
       tbl01.otaxpcat,
       tbl01.stamppcat,
       tbl01.feestype,
       tbl01.invdtype,
       if(tbl01.mydt10p00=1, tbl01.mydt61_p00, '') as mydt61_p00,
       if(tbl01.mydt10p00=2, tbl01.mydt62_p00, '') as mydt62_p00,
       if(   wthhldpcat=''
          && otaxpcat=''
          && feestype=''
          && stamppcat='', tbl01.t09p06, 0) as netvalue, 
       
       /*v1.18*/ /**abs only to wthhldcat**/
       if(wthhldpcat<>'', abs(tbl01.t09p07), tbl01.t09p07) as fpavalue,        
       if(wthhldpcat<>'',  abs(tbl01.t09p06), 0) as wthhld_am, 
       
       if(otaxpcat  <>'',  tbl01.t09p06, 0) as otax_am, 
       if(feestype  <>'',  tbl01.t09p06, 0) as fees_am,
       if(stamppcat <>'',  tbl01.t09p06, 0) as stampvalue
       
FROM  
(
     /************************************
          tbl01=final records
     ************************************/
     SELECT tbl00.*, 
            if(mydt57_p00_2='', mydt57_p00_1, mydt57_p00_2) as mydt57_p00,
            if(mydt58_p00_2='', mydt58_p00_1, mydt58_p00_2) as mydt58_p00,
            if(vatcat_2='',     vatcat_1,     vatcat_2)     as vatcat,
            if(vatexcat_2='',   vatexcat_1,   vatexcat_2)     as vatexcat,
            if(wthhldpcat_2='', wthhldpcat_1, wthhldpcat_2) as wthhldpcat,
            if(otaxpcat_2='',   otaxpcat_1,   otaxpcat_2)   as otaxpcat,
            if(stamppcat_2='',  stamppcat_1,  stamppcat_2)  as stamppcat,
            if(feestype_2='',   feestype_1,   feestype_2)   as feestype,
            if(mydt61_p00_2='', mydt61_p00_1, mydt61_p00_2) as mydt61_p00,
            if(mydt62_p00_2='', mydt62_p00_1, mydt62_p00_2) as mydt62_p00
           
     FROM
     (
     
          /*********************************************************
                 tbl00=find all transactions
          *********************************************************/
          /*v1.18*/ /* no abs*/
          select if(t09.p06=0 && t09.p07=0, t09.p03, t09.p06 ) as t09p06, 
                 t09.p07 as t09p07,
                 t09.p03 as t09p03, 
                 mydt10.p00 as mydt10p00,

                 't09' as tbl, 
                 t09.nr01 as tbl_nr01,            
                 mydt10.tbl as ptbl, 
                 mydt10.tbl_nr01 as ptbl_nr01, 
                 t09.p01 as comments,
                 '' as invdtype,
           
               
                 /*** find data from type (i10) ***/ 
                 ifnull( (select mydt_in.p00 
                          from mydt57 as mydt_in, 
                               q31 as q31_in
                          where q31_in.p01=620020
                             && q31_in.p03=t11.p016
                             && q31_in.p05=mydt_in.p00
                          limit 1), '') as mydt57_p00_1,
                            
                 ifnull( (select mydt_in.p00 
                          from mydt58 as mydt_in, 
                               q31 as q31_in
                          where q31_in.p01=620021
                             && q31_in.p03=t11.p016
                             && q31_in.p05=mydt_in.p00
                          limit 1), '') as mydt58_p00_1,
       
                 ifnull( (select mydt_in.p00 
                          from mydt51 as mydt_in, 
                               q31 as q31_in
                          where q31_in.p01=620024
                             && q31_in.p03=i13.p01
                             && q31_in.p05=mydt_in.p00
                          limit 1), '') as vatcat_1,

                 ifnull( (select mydt_in.p00 
                          from mydt53 as mydt_in, 
                               q31 as q31_in
                          where q31_in.p01=620026
                             && q31_in.p03=t11.p016
                             && q31_in.p05=mydt_in.p00
                          limit 1), '') as wthhldpcat_1,

                 ifnull( (select mydt_in.p00 
                          from mydt54 as mydt_in, 
                               q31 as q31_in
                          where q31_in.p01=620027
                             && q31_in.p03=t11.p016
                             && q31_in.p05=mydt_in.p00
                          limit 1), '') as otaxpcat_1,

                 ifnull( (select mydt_in.p00 
                          from mydt55 as mydt_in, 
                               q31 as q31_in
                          where q31_in.p01=620028
                             && q31_in.p03=t11.p016
                             && q31_in.p05=mydt_in.p00
                          limit 1), '') as stamppcat_1,

                 ifnull( (select mydt_in.p00 
                          from mydt56 as mydt_in, 
                               q31 as q31_in
                          where q31_in.p01=620029
                             && q31_in.p03=t11.p016
                             && q31_in.p05=mydt_in.p00
                          limit 1), '') as feestype_1,
       
                 ifnull( (select mydt_in.p00 
                          from mydt61 as mydt_in, 
                               q31 as q31_in
                          where q31_in.p01=620022
                             && q31_in.p03=t11.p016
                             && q31_in.p05=mydt_in.p00
                          limit 1), '') as mydt61_p00_1,
       
                 ifnull( (select mydt_in.p00 
                          from mydt62 as mydt_in, 
                               q31 as q31_in
                          where q31_in.p01=620023
                             && q31_in.p03=t11.p016
                             && q31_in.p05=mydt_in.p00
                          limit 1), '') as mydt62_p00_1,

                 ifnull( (select mydt_in.p00 
                          from mydt52 as mydt_in, 
                               q31 as q31_in
                          where q31_in.p01=620050
                             && q31_in.p03=t11.p016
                             && q31_in.p05=mydt_in.p00
                          limit 1), '') as vatexcat_1,

                 /*** find data from logi account l90 ***/ 
                 ifnull( (select mydt_in.p00 
                          from mydt57 as mydt_in, 
                               q31 as q31_in
                          where q31_in.p01=620001
                             && q31_in.p03=t09.p08
                             && q31_in.p05=mydt_in.p00
                          limit 1), '') as mydt57_p00_2,
                            
                 ifnull( (select mydt_in.p00 
                          from mydt58 as mydt_in, 
                               q31 as q31_in
                          where q31_in.p01=620002
                             && q31_in.p03=t09.p08
                             && q31_in.p05=mydt_in.p00
                          limit 1), '') as mydt58_p00_2,
       
                 ifnull( (select mydt_in.p00 
                          from mydt51 as mydt_in, 
                               q31 as q31_in
                          where q31_in.p01=620003
                             && q31_in.p03=t09.p08
                             && q31_in.p05=mydt_in.p00
                          limit 1), '') as vatcat_2,

                 ifnull( (select mydt_in.p00 
                          from mydt53 as mydt_in, 
                               q31 as q31_in
                          where q31_in.p01=620005
                             && q31_in.p03=t09.p08
                             && q31_in.p05=mydt_in.p00
                          limit 1), '') as wthhldpcat_2,

                 ifnull( (select mydt_in.p00 
                          from mydt54 as mydt_in, 
                               q31 as q31_in
                          where q31_in.p01=620006
                             && q31_in.p03=t09.p08
                             && q31_in.p05=mydt_in.p00
                          limit 1), '') as otaxpcat_2,

                 ifnull( (select mydt_in.p00 
                          from mydt55 as mydt_in, 
                               q31 as q31_in
                          where q31_in.p01=620007
                             && q31_in.p03=t09.p08
                             && q31_in.p05=mydt_in.p00
                          limit 1), '') as stamppcat_2,

                 ifnull( (select mydt_in.p00 
                          from mydt56 as mydt_in, 
                               q31 as q31_in
                          where q31_in.p01=620008
                             && q31_in.p03=t09.p08
                             && q31_in.p05=mydt_in.p00
                          limit 1), '') as feestype_2,
       
                 ifnull( (select mydt_in.p00 
                          from mydt61 as mydt_in, 
                               q31 as q31_in
                          where q31_in.p01=620010
                             && q31_in.p03=t09.p08
                             && q31_in.p05=mydt_in.p00
                          limit 1), '') as mydt61_p00_2,
       
                 ifnull( (select mydt_in.p00 
                          from mydt62 as mydt_in, 
                               q31 as q31_in
                          where q31_in.p01=620011
                             && q31_in.p03=t09.p08
                             && q31_in.p05=mydt_in.p00
                          limit 1), '') as mydt62_p00_2, 

                 ifnull( (select mydt_in.p00 
                          from mydt52 as mydt_in, 
                               q31 as q31_in
                          where q31_in.p01=620004
                             && q31_in.p03=t09.p08
                             && q31_in.p05=mydt_in.p00
                          limit 1), '') as vatexcat_2

          from {p_mydt10tbl1} as mydt10
          left join t09 on t09.p00=mydt10.tbl_nr01
          left join t11 ON t11.p01=t09.p01
          left join i13 on i13.p01=t11.p019
          left join i10 on i10.p01=t11.p016
          where t09.nr01 is not null
             && ifnull(i10.p106, 0)=0
     ) as tbl00
) as tbl01    