WITH akt_rocnik AS (
	SELECT rocnik FROM seminar_rocniky 
	WHERE id=(SELECT aktualni_rocnik_id FROM seminar_nastaveni)
), id_rocniku AS (
	SELECT id,prvni_rok  FROM seminar_rocniky 
	WHERE rocnik=(SELECT * FROM akt_rocnik) OR rocnik=(SELECT * FROM akt_rocnik)-1
), id_cisel AS (
	SELECT seminar_cisla.id FROM seminar_cisla 
	INNER JOIN id_rocniku ON rocnik_id=id_rocniku.id
), problemy AS (
	SELECT seminar_problemy.id FROM seminar_problemy 
	INNER JOIN id_cisel ON cislo_zadani_id = id_cisel.id
), resitele AS(
	SELECT DISTINCT resitel_id FROM seminar_reseni 
	INNER JOIN problemy ON problem_id=problemy.id
)
SELECT pohlavi_muz,jmeno,prijmeni,NULL AS skola,ulice,mesto,psc,stat FROM seminar_resitele 
INNER JOIN resitele ON seminar_resitele.id=resitel_id
WHERE zasilat='domu' AND rok_maturity > (SELECT MAX(prvni_rok) FROM id_rocniku)
UNION
SELECT res.pohlavi_muz,res.jmeno,res.prijmeni,sk.nazev,sk.ulice,sk.mesto,sk.psc,sk.stat 
FROM seminar_resitele AS res
INNER JOIN resitele ON res.id=resitel_id
INNER JOIN seminar_skoly AS sk ON sk.id=skola_id
WHERE zasilat='do_skoly' AND rok_maturity > (SELECT MAX(prvni_rok) FROM id_rocniku)
ORDER BY prijmeni ASC, jmeno ASC
