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


SELECT tbl01.tbl, 
       tbl01.tbl_nr01, 
       tbl01.ptbl, 
       tbl01.ptbl_nr01, 
       tbl01.comments, 
       tbl01.mydt57_p00,
       tbl01.vatcat,
       tbl01.vatexcat,       
       tbl01.wthhldpcat,
       tbl01.otaxpcat,
       if(tbl01.stamppcat='', tbl01.stamppcat_1, tbl01.stamppcat) as stampcat,
       tbl01.feestype,
       tbl01.invdtype,
       tbl01.mydt61_p00,
       if(   wthhldpcat=''
          && otaxpcat=''  
          && feestype=''  
          && stamppcat='' 
          && stamppcat_1='', i01p011, 0) as netvalue, 
       if(vatcat<>''    , i01fp011, 0) as fpavalue, 
       if(wthhldpcat<>'', i01p011, 0) + wthhld_am as wthhld_am, 
       if(otaxpcat<>''  , i01p011, 0) + otax_am as otax_am, 
       if(feestype<>''  , i01p011, 0) as fees_am,
       if(stamppcat<>'' || stamppcat_1<>'', i01p011, 0) as stampvalue
       
FROM  
(
    /*********************************************************
           transactions
    *********************************************************/
    
    /******************************************************
            tran01.records
    ******************************************************/
    (
    select tran01.p09  as i01p011, 
           tran01.p011 as i01fp011, 
           0 as otax_am, 
           0 as wthhld_am,
           
    
           'tran01' as tbl, 
           tran01.nr01 as tbl_nr01,            
           mydt10.tbl as ptbl, 
           mydt10.tbl_nr01 as ptbl_nr01, 
           '' as comments,
           /**incoming category**/
           ifnull( (select mydt_in.p00 
                    from mydt57 as mydt_in, 
                         q31 as q31_in
                    where q31_in.p01=620031
                       && q31_in.p03=1
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as mydt57_p00,
           
           /**fpa category**/                 
           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,
       
           /* vat exception from invoice (a77)*/
           if( tran01.p011<>0, '', 
                   ifnull( (select mydt_in.p00 
                            from mydt52 as mydt_in, 
                                 q31 as q31_in
                            where q31_in.p01=620051
                               && q31_in.p03=tran01.p124
                               && q31_in.p05=mydt_in.p00
                             limit 1), 
                         '') 
               ) as vatexcat,       

           '' as wthhldpcat,
           '' as otaxpcat,
           '' as stamppcat,
           '' as stamppcat_1,
           '' as feestype,       
           '' as invdtype,
           
           /**e3 category**/
           ifnull( (select mydt_in.p00 
                    from mydt61 as mydt_in, 
                         q31 as q31_in
                    where q31_in.p01=620032
                       && q31_in.p03=1
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as mydt61_p00

    from {p_mydt10tbl1} as mydt10
    left join tran01 on tran01.nr01=mydt10.tbl_nr01
    left join i13 on i13.p01=tran01.p115
    where tran01.nr01 is not null
    group by tran01.nr01
    )
    
) as tbl01    