﻿UPDATE osztaly SET osztaly.C_SZMKKEPVISELOID = NULL 
FROM T_OSZTALY osztaly
INNER JOIN T_TANEV tanev ON osztaly.C_ALTANEVID = tanev.ID AND tanev.C_AKTIV = 'T' AND tanev.TOROLT = 'F'
WHERE osztaly.TOROLT = 'F' AND osztaly.C_SZMKKEPVISELOID IS NOT NULL AND osztaly.C_SZMKKEPVISELOID NOT IN (
  SELECT g.ID
  FROM T_TANULOCSOPORT tcs
  INNER JOIN T_TANULO t on tcs.C_TANULOID = t.ID
  INNER JOIN T_GONDVISELO g on g.C_TANULOID = t.ID
  WHERE tcs.TOROLT = 'F' and t.TOROLT = 'F' and g.TOROLT = 'F'
    AND tcs.C_OSZTALYCSOPORTID = osztaly.ID
    AND GETDATE() BETWEEN tcs.C_BELEPESDATUM AND ISNULL(tcs.C_KILEPESDATUM, GETDATE())
)

UPDATE osztaly SET osztaly.C_SZMKKEPVISELOHELYETTESID = NULL 
FROM T_OSZTALY osztaly
INNER JOIN T_TANEV tanev ON osztaly.C_ALTANEVID = tanev.ID AND tanev.C_AKTIV = 'T' AND tanev.TOROLT = 'F'
WHERE osztaly.TOROLT = 'F' AND osztaly.C_SZMKKEPVISELOHELYETTESID IS NOT NULL AND osztaly.C_SZMKKEPVISELOHELYETTESID NOT IN (
  SELECT g.ID
  FROM T_TANULOCSOPORT tcs
  INNER JOIN T_TANULO t on tcs.C_TANULOID = t.ID
  INNER JOIN T_GONDVISELO g on g.C_TANULOID = t.ID
  WHERE tcs.TOROLT = 'F' and t.TOROLT = 'F' and g.TOROLT = 'F'
    AND tcs.C_OSZTALYCSOPORTID = osztaly.ID
    AND GETDATE() BETWEEN tcs.C_BELEPESDATUM AND ISNULL(tcs.C_KILEPESDATUM, GETDATE())
)