SELECT firstname, lastname, count(*), max(teacher_uco), min(teacher_uco) FROM teacher t GROUP BY firstname, lastname HAVING count(*) > 1 DELETE FROM teacher t WHERE EXISTS ( SELECT 1 FROM teacher t2 WHERE t.firstname = t2.firstname AND t.lastname = t2.lastname AND t2.teacher_uco < t.teacher_uco ) UPDATE teacher SET datumnarozeni = ( SELECT datumnarozeni FROM tdn WHERE teacher.teacher_uco = tdn.uco ) CREATE TABLE OSA as SELECT TO_char(to_date('2003-09-01','yyyy-mm-dd') + a * INTERVAL '1 month', 'yyyy-mm') mesic FROM generate_series (1,100) a SELECT osa.mesic, count(patient_id) FROM OSA LEFT JOIN patient_study ps ON osa.mesic = to_char(date_of_enrollment,'yyyy-mm') AND study_id = 23 GROUP BY osa.mesic ORDER BY osa.mesic SELECT pohlavicsu,nazev, okresbydliste, abs(avg(vekcsu) - LEAD(avg(vekcsu), 1, NULL) OVER (PARTITION BY okresbydliste ORDER BY lpz_csu.pohlavicsu)) rozdil FROM lpz_csu LEFT join krajokres ON krajokres.kod = lpz_csu.okresbydliste GROUP BY okresbydliste, krajokres.nazev, lpz_csu.pohlavicsu ORDER BY okresbydliste, lpz_csu.pohlavicsu