
SELECT p01 as p01,
      sum(ypol) as ypol,
      sum(papel) as papel,
      sum(papro) as papro
FROM
(

/* ypoloipo */
select
    i00tot.pno1 as p01,
    i00tot.p03  as ypol,
    0 as papel,
    0 as papro
FROM i00tot
WHERE i00tot.pno1='{par_pararr[1]}' && i00tot.p610='{par_pararr[3]}'
GROUP BY i00tot.pno1

UNION ALL

/* parag pelath */
SELECT a58.v101 as p01,
       0 as ypol,
       sum(a58.v103-a58.v109) as papel,
       0 as papro
FROM a58, a57
WHERE a58.v101='{par_pararr[1]}'
   && a58.v103>a58.v109
   && a58.p00=a57.nr01
   && a57.p591='0'
   && if(a58.vapcl1=1, a58.vapc1, a57.p703)='{par_pararr[3]}'
GROUP BY a58.v101

UNION ALL

/*parag promht*/
SELECT a68.v101 as p01,
       0 as ypol,
       0 as papel,
       sum(a68.v103-a68.v109) as papro
FROM a68, a67
WHERE a68.v101='{par_pararr[1]}'
   && a68.v103>a68.v109
   && a68.p00=a67.nr01
   && a67.p591='0'
   && if(a68.vapcl1=1, a68.vapc1, a67.p703)='{par_pararr[3]}'
GROUP BY a68.v101
) as t1
GROUP BY p01;