﻿UPDATE z
SET TOROLT = 'T', MODIFIER = 0, LASTCHANGED = GETDATE(), SERIAL += 1
FROM T_ZARADEK z
      INNER JOIN T_TANULOCSOPORT tcs ON z.C_TANULOCSOPORTID = tcs.ID AND tcs.TOROLT = 'F'
      INNER JOIN T_TANEV tn ON tn.ID = z.C_TANEVID AND tn.TOROLT = 'F'
WHERE z.TOROLT = 'F'
  AND tcs.C_TANEVID <> z.C_TANEVID
  AND exists (SELECT 1
              FROM T_ZARADEK z2
              WHERE z2.C_TANULOCSOPORTID = z.C_TANULOCSOPORTID
                AND z2.C_TANEVID = tcs.C_TANEVID
                AND z2.C_SZOVEG = z.C_SZOVEG
             )

UPDATE z
SET C_TANEVID = tcs.C_TANEVID, MODIFIER = 0, LASTCHANGED = GETDATE(), SERIAL += 1
FROM T_ZARADEK z
      INNER JOIN T_TANULOCSOPORT tcs ON z.C_TANULOCSOPORTID = tcs.ID AND tcs.TOROLT = 'F'
      INNER JOIN T_TANEV tn ON tn.ID = z.C_TANEVID AND tn.TOROLT = 'F'
      INNER JOIN T_CSOPORT cs on cs.ID = tcs.C_OSZTALYCSOPORTID and cs.TOROLT = 'F'
WHERE z.TOROLT = 'F'
  AND tcs.C_TANEVID <> z.C_TANEVID
  AND not exists (select 1
                  from T_ZARADEK z2
                  where z2.C_TANULOCSOPORTID = z.C_TANULOCSOPORTID
                    and z2.C_TANEVID = tcs.C_TANEVID
                 )