select "Konto", case when "Soll" is null then 0 else "Soll" end, case when "Haben" is null then 0 else "Haben" end from ((select ktnr as "Konto", sum(dm) as "Soll" from ktp where dat <= '2004-12-31' group by "Konto") as t1 natural full join (select gkto as "Konto", sum(dm) as "Haben" from ktp where dat <= '2004-12-31' group by "Konto") as t2);