SELECT i11.nr01 as nr01, 
       i11.p02 as p02, 
       i11.p01 as p01, 
       i00.name as vp01, 
       i11.p03 as p03, 
       i11.p04 as p04, 
       i11.p09 as p09, 
       i11.p010 as p010, 
       i11.p011 as p011, 
       i11.p012 as p012, 
       i11.timh1 as timh1, 
       i11.timh4 as timh4, 
       i11.timh3 as timh3, 
       i11.timh2 as timh2, 
       i11.p013 as p013, 
       i11.p302 as p302, 
       i11.p303 as p303, 
       i11.p304 as p304, 
       i11.p306 as p306, 
       i11.p308 as p308, 
       i52.p02 as vp308, 
       i11.p101 as p101, 
       ifnull(lota02_1.p02, '') as vp101, 
       i11.p102 as p102, 
       ifnull(lota02_2.p02, '') as vp102, 
       i11.p103 as p103, 
       ifnull(lota02_3.p02, '') as vp103, 
       
       i11.remdate  as remdate, 
       i11.remtime  as remtime, 
       i11.remuser  as remuser, 
       i11.remfdate  as remfdate, 
       i11.remftime  as remftime, 
       i11.remfuser  as remfuser, 
       i11.remguc  as remguc, 
       sum( ifnull(if(i38_d01.p610>={peg_xoro1} && i38_d01.p610<={peg_xoro2}, i38_d01.poseis-i38_d01.posex, 0), 0 )) as vp_ypol1, 
       sum( ifnull(i38_d01.poseis-i38_d01.posex, 0 )) as vp_ypol2 
FROM i11  
LEFT JOIN i00 ON i00.pno=i11.p01 
LEFT JOIN i52 ON i52.p01=i11.p308
LEFT JOIN lota02 as lota02_1 ON lota02_1.p01=i11.p101
LEFT JOIN lota02 as lota02_2 ON lota02_2.p01=i11.p102
LEFT JOIN lota02 as lota02_3 ON lota02_3.p01=i11.p103
LEFT JOIN ({peg_str}) as i38_d01  ON i38_d01.p01=i11.p01
                                  && i38_d01.p02=i11.p02
             
WHERE i11.p01='{nv101}'
   && i11.p012=0 
GROUP BY i11.p01, i11.p02   ;