INSERT INTO _import_optical_groups (lang_0000040e, fajta_kod, optical, nev_elozo, nev_egyutt)

(SELECT dat.lang_0000040e, dat.fajta_kod, 1 as optical, dat.nev_elozo, dat.nev_egyutt
  FROM (SELECT fajta2_nev as lang_0000040e, 2 as fajta_kod, '' as nev_elozo, '' as nev_egyutt FROM _import_optical_items WHERE fajta2_nev <> '' GROUP BY 1
UNION
SELECT fajta3_nev, 3 as szint, fajta2_nev as nev_elozo, fajta2_nev as nev_egyutt FROM _import_optical_items WHERE fajta3_nev <> '' GROUP BY 1,3,4
UNION
SELECT fajta4_nev, 4 as szint, fajta3_nev as nev_elozo, fajta3_nev||'×'||fajta2_nev as nev_egyutt FROM _import_optical_items WHERE fajta4_nev <> '' GROUP BY 1,3,4
UNION
SELECT fajta5_nev, 5 as szint, fajta4_nev as nev_elozo, fajta4_nev||'×'||fajta3_nev||'×'||fajta2_nev as nev_egyutt FROM _import_optical_items WHERE fajta5_nev <> '' GROUP BY 1,3,4
UNION
SELECT fajta6_nev, 6 as szint, fajta5_nev as nev_elozo, fajta5_nev||'×'||fajta4_nev||'×'||fajta3_nev||'×'||fajta2_nev as nev_egyutt FROM _import_optical_items WHERE fajta6_nev <> '' GROUP BY 1,3,4) dat
  LEFT JOIN _import_optical_groups tf ON tf.optical = 1 AND tf.lang_0000040e = dat.lang_0000040e AND tf.nev_egyutt = dat.nev_egyutt
  WHERE tf.kod is null)