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, 
       tbl01.mydt57_p00,
       tbl01.mydt58_p00,
       tbl01.vatcat,
       tbl01.vatexcat,       
       tbl01.wthhldpcat,
       tbl01.otaxpcat,
       tbl01.stamppcat,
       tbl01.feestype,
       tbl01.invdtype,
       tbl01.mydt61_p00,
       tbl01.mydt62_p00, 
       if(l90p910=1 && l90p911=3, l93poso, 0) as netvalue, 
       if(l90p910=1 && l90p911=4, l93poso, 0) as fpavalue, 
       if(wthhldpcat<>'', l93poso, 0) as wthhld_am, 
       if(otaxpcat   <>'', l93poso, 0) as otax_am, 
       if(feestype  <>'', l93poso, 0) as fees_am,
       if(stamppcat   <>'', l93poso, 0) as stampvalue
       
FROM  
(
    /*********************************************************
           find all transactions
    *********************************************************/
    select l93.p01 as l93p01, 
           l93.p03 + l93.p04 as l93poso,
           l90.p910 as l90p910, 
           l90.p911 as l90p911,
           
           '{p_mydt10arr[2]}' as tbl, 
           l93.nr01 as tbl_nr01,            
           mydt10.tbl as ptbl, 
           mydt10.tbl_nr01 as ptbl_nr01, 
           ifnull(l90.nr01, 0) as l90nr01, 
           l90.p001 as comments,
           
           ifnull( (select mydt_in.p00 
                    from mydt57 as mydt_in, 
                         q31 as q31_in
                    where q31_in.p01=620001
                       && q31_in.p03=l90.p001
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as mydt57_p00,
                            
           ifnull( (select mydt_in.p00 
                    from mydt58 as mydt_in, 
                         q31 as q31_in
                    where q31_in.p01=620002
                       && q31_in.p03=l90.p001
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as mydt58_p00,
       
           ifnull( (select mydt_in.p00 
                    from mydt51 as mydt_in, 
                         q31 as q31_in
                    where q31_in.p01=620003
                       && q31_in.p03=l90.p001
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as vatcat,
       
           ifnull( (select mydt_in.p00 
                    from mydt52 as mydt_in, 
                         q31 as q31_in
                    where q31_in.p01=620004
                       && q31_in.p03=l90.p001
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as vatexcat,       

           ifnull( (select mydt_in.p00 
                    from mydt53 as mydt_in, 
                         q31 as q31_in
                    where q31_in.p01=620005
                       && q31_in.p03=l90.p001
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as wthhldpcat,

           ifnull( (select mydt_in.p00 
                    from mydt54 as mydt_in, 
                         q31 as q31_in
                    where q31_in.p01=620006
                       && q31_in.p03=l90.p001
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as otaxpcat,

           ifnull( (select mydt_in.p00 
                    from mydt55 as mydt_in, 
                         q31 as q31_in
                    where q31_in.p01=620007
                       && q31_in.p03=l90.p001
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as stamppcat,

           ifnull( (select mydt_in.p00 
                    from mydt56 as mydt_in, 
                         q31 as q31_in
                    where q31_in.p01=620008
                       && q31_in.p03=l90.p001
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as feestype,
       
           ifnull( (select mydt_in.p00 
                    from mydt60 as mydt_in, 
                         q31 as q31_in
                    where q31_in.p01=620009
                       && q31_in.p03=l90.p001
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as invdtype,
       
           ifnull( (select mydt_in.p00 
                    from mydt61 as mydt_in, 
                         q31 as q31_in
                    where q31_in.p01=620010
                       && q31_in.p03=l90.p001
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as mydt61_p00,
       
           ifnull( (select mydt_in.p00 
                    from mydt62 as mydt_in, 
                         q31 as q31_in
                    where q31_in.p01=620011
                       && q31_in.p03=l90.p001
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as mydt62_p00


    from {p_mydt10tbl1} as mydt10
    left join {p_mydt10arr[2]} as l93 on l93.p00=mydt10.tbl_nr01
    left join l90 ON l90.p001=l93.p01
    where l90.nr01 is not null
) as tbl01    