select cast(p01 as date) as p01,
       p02 as p02,
       p03 as p03,
       @n1:=@n1 + p03 as sp03
FROM
(

select '01000101' as p01,
       '{sysmsg(134, "-")}' as p02,
       sum(tab_01.p03) as p03
from 
     /*erpa80_d03 */
    ( {peg_str1} ) 
as tab_01
WHERE tab_01.nr01={nt_p01}
  &&  tab_01.p01<'{odbcdtos(nt_d1)}'
GROUP BY nr01

UNION ALL

select p01, p02, sum(p03) as p03
from   
     /*erpa80_d03*/
    ( {peg_str1} )
as tab_02     
WHERE tab_02.nr01={nt_p01}
  &&  tab_02.p01>='{odbcdtos(nt_d1)}'
  &&  tab_02.p01<='{odbcdtos(nt_d2)}'
GROUP BY p01, p02, p04
ORDER BY p01
) as t1;
