SELECT *
FROM
(
select
  0 as type,
  d70.p08 as kat,
  d77.p02 as vkat,
  sum(1) as plit,
  sum(d70.p04) as axia,
  0 as nr01,
  0 as p00,
  '' as vp00,
  '' as p01,
  curdate() as p02,
  0 as p04,
  0 as p07
FROM d00, d70
LEFT JOIN d77 ON d77.p01=d70.p08
WHERE d70.p00=d00.nr01
  &&  d70.p02>='{dtos(np_df)}'
  &&  d70.p02<='{dtos(np_dt)}'
  && d00.p900 ='{np_pol}'
GROUP BY d77.p02

UNION
select
  1 as type,
  d70.p08 as kat,
  '' as vkat,
  0 as plit,
  0 as axia,
  d70.nr01 as nr01,
  d70.p00 as p00,
  d00.p19 as vp00,
  d70.p01 as p01,
  d70.p02 as p02,
  d70.p04 as p04,
  d70.p07 as p07
FROM d00, d70
WHERE d70.p00=d00.nr01
  &&  d70.p02>='{dtos(np_df)}'
  &&  d70.p02<='{dtos(np_dt)}'
  && d00.p900 ='{np_pol}'
  && d70.p08 IN ( {p_plain} 0)
) as t1
order by t1.kat, t1.type ;
