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,
       i01p011 as netvalue, 
       i01fp011 as fpavalue, 
       wthhld_am as wthhld_am, 
       otax_am as otax_am, 
       fees_am as fees_am,
       0 as stampvalue
       
FROM  
(
    /*********************************************************
           transactions
    *********************************************************/
    
    /******************************************************
            eleo20.records (net && vat)
    ******************************************************/
    (
    select eleo20.ptax01  as i01p011, 
           eleo20.ptax02  as i01fp011, 
           0 as otax_am, 
           0 as wthhld_am,
           0 as fees_am,
               
           'eleo20' as tbl, 
           eleo20.nr01 as tbl_nr01,            
           mydt10.tbl as ptbl, 
           mydt10.tbl_nr01 as ptbl_nr01, 
           '' as comments,
           
           ifnull( (select mydt_in.p00 
                    from mydt57 as mydt_in, 
                         q31 as q31_in
                    where q31_in.p01=620034
                       && q31_in.p03=1
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as mydt57_p00,
                            
           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,
       
           '' as vatexcat,       
           '' as wthhldpcat,
           '' as otaxpcat,
           '' as stamppcat,
           '' as stamppcat_1,
           '' as feestype,       
           '' as invdtype,
       
           ifnull( (select mydt_in.p00 
                    from mydt61 as mydt_in, 
                         q31 as q31_in
                    where q31_in.p01=620035
                       && q31_in.p03=1
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as mydt61_p00

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

    UNION ALL
    
    /******************************************************
            eleo20.records (δακοκτονία)
    ******************************************************/
    (
    select 0  as i01p011, 
           0  as i01fp011, 
           0 as otax_am, 
           0 as wthhld_am,
           eleo20.p103e as fees_am,
               
           'eleo20' as tbl, 
           eleo20.nr01 as tbl_nr01,            
           mydt10.tbl as ptbl, 
           mydt10.tbl_nr01 as ptbl_nr01, 
           '' as comments,
           
           ifnull( (select mydt_in.p00 
                    from mydt57 as mydt_in, 
                         q31 as q31_in
                    where q31_in.p01=620034
                       && q31_in.p03=1
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as mydt57_p00,
                            
           '7' as vatcat,
           '21' as vatexcat,       
           '' as wthhldpcat,
           '' as otaxpcat,
           '' as stamppcat,
           '' as stamppcat_1,
           '9' as feestype,       
           '' as invdtype,
       
           ifnull( (select mydt_in.p00 
                    from mydt61 as mydt_in, 
                         q31 as q31_in
                    where q31_in.p01=620035
                       && q31_in.p03=1
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as mydt61_p00

    from {p_mydt10tbl1} as mydt10
    left join eleo20 on eleo20.nr01=mydt10.tbl_nr01
    left join i13 on i13.p01=eleo20.p708
    where eleo20.nr01 is not null
       && eleo20.p103e<>0
    group by eleo20.nr01
    )    

    
) as tbl01    