SELECT *
FROM
(
  SELECT mydt10.nr01, 
         mydt10.uid as uid,
         mydt10.mark as mark,
         mydt10.dmark as dmark, 
         if(mydt10.series='', '0', mydt10.series) as series,
         mydt10.aa as aa
   FROM mydt12, mydt10
   LEFT JOIN t07 ON t07.p01=mydt10.p03
   LEFT JOIN t05 ON t05.p01=t07.p07
   WHERE mydt12.mydt10=mydt10.nr01
      && mydt10.mark<>''
      && mydt10.dmark=''
      && mydt10.p01=1
      && mydt10.p05=1
      && mydt10.p09=0
      && (    ifnull(t05.p01, -999999)={sme_t05p01}
           || {sme_t05p01}=-1
          ) 
      && ( {np_where} 
         )
   GROUP BY mydt10.nr01
) as tbl01;
