\encoding UTF8
COPY (SELECT query_to_xml('SELECT * FROM (
SELECT f.kod as kr2007_fajta_kod, 0 as parent_kod                , f.webboltban_nev as name, (SELECT count(*) FROM tetel WHERE tetel.fajta1 = f.kod GROUP BY tetel.fajta1) as item_count
  FROM tetel_fajtak f
  WHERE f.fajta_kod = 1 AND f.ures = 0 AND (not(f.kod = 0))
UNION
SELECT f.kod as kr2007_fajta_kod, f.lanc_kapcsolt_kod as parent_kod, f.webboltban_nev as name, (SELECT count(*) FROM tetel WHERE tetel.fajta2 = f.kod GROUP BY tetel.fajta2) as item_count
  FROM tetel_fajtak f
  LEFT JOIN tetel_fajtak tf ON tf.kod = f.lanc_kapcsolt_kod
  WHERE f.fajta_kod = 2 AND f.ures = 0 AND (not(f.kod = 0))
   AND (not(tf.kod is null))
UNION
SELECT f.kod as kr2007_fajta_kod, f.lanc_kapcsolt_kod as parent_kod, f.webboltban_nev as name, (SELECT count(*) FROM tetel WHERE tetel.fajta3 = f.kod GROUP BY tetel.fajta3) as item_count
  FROM tetel_fajtak f
  LEFT JOIN tetel_fajtak tf ON tf.kod = f.lanc_kapcsolt_kod
  WHERE f.fajta_kod = 3 AND f.ures = 0 AND (not(f.kod = 0))
   AND (not(tf.kod is null))
UNION
SELECT f.kod as kr2007_fajta_kod, f.lanc_kapcsolt_kod as parent_kod, f.webboltban_nev as name, (SELECT count(*) FROM tetel WHERE tetel.fajta4 = f.kod GROUP BY tetel.fajta4) as item_count
  FROM tetel_fajtak f
  LEFT JOIN tetel_fajtak tf ON tf.kod = f.lanc_kapcsolt_kod
  WHERE f.fajta_kod = 4 AND f.ures = 0 AND (not(f.kod = 0))
   AND (not(tf.kod is null))
UNION
SELECT f.kod as kr2007_fajta_kod, f.lanc_kapcsolt_kod as parent_kod, f.webboltban_nev as name, (SELECT count(*) FROM tetel WHERE tetel.fajta5 = f.kod GROUP BY tetel.fajta5) as item_count
  FROM tetel_fajtak f
  LEFT JOIN tetel_fajtak tf ON tf.kod = f.lanc_kapcsolt_kod
  WHERE f.fajta_kod = 5 AND f.ures = 0 AND (not(f.kod = 0))
   AND (not(tf.kod is null))
UNION
SELECT f.kod as kr2007_fajta_kod, f.lanc_kapcsolt_kod as parent_kod, f.webboltban_nev as name, (SELECT count(*) FROM tetel WHERE tetel.fajta6 = f.kod GROUP BY tetel.fajta6) as item_count
  FROM tetel_fajtak f
  LEFT JOIN tetel_fajtak tf ON tf.kod = f.lanc_kapcsolt_kod
  WHERE f.fajta_kod = 6 AND f.ures = 0 AND (not(f.kod = 0))
   AND (not(tf.kod is null))
ORDER BY 2,3) as data
WHERE (not(name = ''''))', true, false, '')) to '/tmp/flexovit_fajta.xml'