\encoding UTF8
COPY (SELECT query_to_xml('SELECT kod as customer_id, nev as name,
  partner_csoport_kod as customer_group,
  (SELECT _59_nev_bontas(nev,1) FROM partner_emailtar WHERE partner_emailtar.partner_kod = partner.kod ORDER BY alapertelmezett LIMIT 1) as person_last_name, 
  (SELECT _59_nev_bontas(nev,2) FROM partner_emailtar WHERE partner_emailtar.partner_kod = partner.kod ORDER BY alapertelmezett LIMIT 1) as person_first_name, 
  partner.webbolt_userid as email, 
  partner.telefon as telephone,
  (SELECT SUM(kedvezmeny) FROM kedvezmeny, partner_kedvezmeny WHERE kedvezmeny.kod = partner_kedvezmeny.kedvezmeny_kod AND partner_kedvezmeny.partner_kod = partner.kod) as discount,
  partner.fizetesmod as kr_payment_method,
  (SELECT munkatars.megjegyzes
     FROM uzletkoto
     LEFT JOIN munkatars ON munkatars.kod = uzletkoto.munkatars_kod
    WHERE uzletkoto.kod = partner.uzletkoto_kod) as dealer_email
  FROM 
   partner
  WHERE 
   (not(webbolt_userid = '''')) and (not(webbolt_userid is null)) and (not(webbolt_userid = ''-''))
    AND ((partner.lasteditedtime_forsync + interval ''12 hours'') > current_timestamp)
  ORDER BY kod
', true, false, '')) to '/tmp/flexovit_partner_changed.xml'