select *
FROM
(

(select
  0 as type,
  d70.p08 as kat,
  d77.p02 as vkat,
  sum(1) as plit,
  sum(d70.p04) as axia
FROM d70
LEFT JOIN d77 ON d77.p01=d70.p08
WHERE d70.p00='{nnr01}'
  &&  d70.p02<'{dtos(date())}'
GROUP BY d77.p02
)

UNION

(select
  1 as type,
  d70.p08 as kat,
  d77.p02 as vkat,
  sum(1) as plit,
  sum(d70.p04) as axia
FROM d70
LEFT JOIN d77 ON d77.p01=d70.p08
WHERE d70.p00='{nnr01}'
  &&  d70.p02>='{dtos(date())}'
GROUP BY d77.p02
)

) as t1
order by t1.type, t1.kat;
