select 'Hochschulen', 0::char(10) as hs_nr, null::char(10) as parent, 0::smallint as ebene from xdummy
union
select name, hs_nr::char(10), '0'::char(10) as parent, 1::smallint as ebene from sxc_hochschulen where
hs_nr is not null and hsk_laufnummer not in (select hsk_laufnummer from sxc_hochschulen_standorte where hauptsitz='1')
union
select name, 'ST_'||hs_nr::char(10), '0'::char(10) as parent, 1::smallint as ebene from sxc_hochschulen where
hs_nr is not null and hsk_laufnummer in (select hsk_laufnummer from sxc_hochschulen_standorte where hauptsitz='1')
--und die Standorte
union select trim(name)|| ' Standort '||standort_ort, hs_nr::char(10), 'ST_'||hs_nr_hauptsitz as parent, 1::smallint as ebene from sxc_hochschulen_standorte
where hs_nr_hauptsitz is not null and hs_nr is not null;