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
    *********************************************************/
    
    /******************************************************
            c78.records
    ******************************************************/
    ( /**vat cat=1**/
    select c78.p301  as i01p011, 
           c78.p201  as i01fp011, 
           0 as otax_am, 
           0 as wthhld_am,
           0 as fees_am,
               
           'c78' as tbl, 
           c78.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=620037
                       && 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=620038
                       && q31_in.p03=1
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as mydt61_p00

    from {p_mydt10tbl1} as mydt10
    left join c78 on c78.nr01=mydt10.tbl_nr01
    left join i13 on i13.p01=1
    where c78.nr01 is not null
       && c78.p301<>0
    group by c78.nr01
    )
    
    UNION ALL
    
    ( /**vat cat=2**/
    select c78.p302  as i01p011, 
           c78.p202  as i01fp011, 
           0 as otax_am, 
           0 as wthhld_am,
           0 as fees_am,
               
           'c78' as tbl, 
           c78.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=620037
                       && 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=620038
                       && q31_in.p03=1
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as mydt61_p00

    from {p_mydt10tbl1} as mydt10
    left join c78 on c78.nr01=mydt10.tbl_nr01
    left join i13 on i13.p01=2
    where c78.nr01 is not null
       && c78.p302<>0
    group by c78.nr01
    )
        
    UNION ALL
    
    ( /**vat cat=3**/
    select c78.p303  as i01p011, 
           c78.p203  as i01fp011, 
           0 as otax_am, 
           0 as wthhld_am,
           0 as fees_am,
               
           'c78' as tbl, 
           c78.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=620037
                       && 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=620038
                       && q31_in.p03=1
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as mydt61_p00

    from {p_mydt10tbl1} as mydt10
    left join c78 on c78.nr01=mydt10.tbl_nr01
    left join i13 on i13.p01=3
    where c78.nr01 is not null
       && c78.p303<>0
    group by c78.nr01
    )
        
        UNION ALL
    
    ( /**vat cat=4**/
    select c78.p304  as i01p011, 
           c78.p204  as i01fp011, 
           0 as otax_am, 
           0 as wthhld_am,
           0 as fees_am,
               
           'c78' as tbl, 
           c78.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=620037
                       && 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=620038
                       && q31_in.p03=1
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as mydt61_p00

    from {p_mydt10tbl1} as mydt10
    left join c78 on c78.nr01=mydt10.tbl_nr01
    left join i13 on i13.p01=4
    where c78.nr01 is not null
       && c78.p304<>0
    group by c78.nr01
    )
        
    UNION ALL
    
    ( /**vat cat=5**/
    select c78.p305 as i01p011, 
           c78.p205  as i01fp011, 
           0 as otax_am, 
           0 as wthhld_am,
           0 as fees_am,
               
           'c78' as tbl, 
           c78.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=620037
                       && 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=620038
                       && q31_in.p03=1
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as mydt61_p00

    from {p_mydt10tbl1} as mydt10
    left join c78 on c78.nr01=mydt10.tbl_nr01
    left join i13 on i13.p01=5
    where c78.nr01 is not null
       && c78.p305<>0
    group by c78.nr01
    )
        
    UNION ALL
    
    ( /**vat cat=6**/
    select c78.p306  as i01p011, 
           c78.p206  as i01fp011, 
           0 as otax_am, 
           0 as wthhld_am,
           0 as fees_am,
               
           'c78' as tbl, 
           c78.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=620037
                       && 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=620038
                       && q31_in.p03=1
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as mydt61_p00

    from {p_mydt10tbl1} as mydt10
    left join c78 on c78.nr01=mydt10.tbl_nr01
    left join i13 on i13.p01=6
    where c78.nr01 is not null
       && c78.p306<>0
    group by c78.nr01
    )
        
    UNION ALL
    
    ( /**vat cat=7**/
    select c78.p307  as i01p011, 
           c78.p207  as i01fp011, 
           0 as otax_am, 
           0 as wthhld_am,
           0 as fees_am,
               
           'c78' as tbl, 
           c78.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=620037
                       && 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=620038
                       && q31_in.p03=1
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as mydt61_p00

    from {p_mydt10tbl1} as mydt10
    left join c78 on c78.nr01=mydt10.tbl_nr01
    left join i13 on i13.p01=7
    where c78.nr01 is not null
       && c78.p307<>0
    group by c78.nr01
    )
        
    UNION ALL
    
    ( /**vat cat=8**/
    select c78.p308  as i01p011, 
           c78.p208  as i01fp011, 
           0 as otax_am, 
           0 as wthhld_am,
           0 as fees_am,
               
           'c78' as tbl, 
           c78.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=620037
                       && 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=620038
                       && q31_in.p03=1
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as mydt61_p00

    from {p_mydt10tbl1} as mydt10
    left join c78 on c78.nr01=mydt10.tbl_nr01
    left join i13 on i13.p01=8
    where c78.nr01 is not null
       && c78.p308<>0
    group by c78.nr01
    )
        
    UNION ALL
    
    ( /**vat cat=9**/
    select c78.p309  as i01p011, 
           c78.p209  as i01fp011, 
           0 as otax_am, 
           0 as wthhld_am,
           0 as fees_am,
               
           'c78' as tbl, 
           c78.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=620037
                       && 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=620038
                       && q31_in.p03=1
                       && q31_in.p05=mydt_in.p00
                    limit 1), '') as mydt61_p00

    from {p_mydt10tbl1} as mydt10
    left join c78 on c78.nr01=mydt10.tbl_nr01
    left join i13 on i13.p01=9
    where c78.nr01 is not null
       && c78.p309<>0
    group by c78.nr01
    )
        
) as tbl01    