SELECT *
FROM
(
  SELECT mydt10.nr01, 
         mydt10.uid as uid,
         mydt10.mark as mark,

         mydt12.nr01 as nr01_1, 
         mydt12.linenumber as lineNumber,
         mydt12.mydt61_p00 as classificationType,
         mydt12.mydt57_p00 as classificationCategory,
         mydt12.netvalue + mydt12.wthhld_am + mydt12.fees_am + mydt12.otax_am as amount,

         ifnull( (select sum(if(mydt12_in.mydt57_p00='' || mydt12_in.mydt61_p00='', 1, 0))
                  from mydt12 as mydt12_in
                  where mydt12_in.mydt10=mydt10.nr01
                  group by mydt12_in.mydt10), 0) as nomarked, 
         ifnull( (select sum(if(mydt12_in.mydt57_p00='' || mydt12_in.mydt61_p00='', 0, 1))
                  from mydt12 as mydt12_in
                  where mydt12_in.mydt10=mydt10.nr01
                  group by mydt12_in.mydt10), 0) as marked
                  
         
   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.entuid=''
      && mydt10.p00=1
      && mydt10.p01=2
      && mydt10.p09=0
      && (    ifnull(t05.p01, -999999)={sme_t05p01}
           || {sme_t05p01}=-1
          ) 
      && {np_where}
   ORDER BY mydt10.issueDate, mydt10.nr01, mydt12.linenumber
) as tbl01
WHERE nomarked=0
   && marked<>0;
