SELECT *
FROM
(

/* q10 records */
(select
   1 as type,
   q10.p04 as kat,
   q09.p01 as vkat,
   q09.p02 as diad,
   q10.p01 as p01,
   q10.p05 as p05,
   q10.nr01 as nr01
FROM q10
LEFT JOIN q09 ON q10.p04=q09.nr01
WHERE q10.p05=1 || (q10.p05=0 && q10.p04<>0)
)

UNION

/* q09 records */
(select
   0 as type,
   q10.p04 as kat,
   q09.p01 as vkat,
   q09.p02 as diad,
   '' as p01,
   0 as p05,
   0 as nr01
FROM q10
LEFT JOIN q09 ON q10.p04=q09.nr01
WHERE q10.p05=1 || (q10.p05=0 && q10.p04<>0)
GROUP BY q10.p04
)

) as t1
ORDER BY t1.diad, t1.type, t1.p01 ;

   