﻿--Migration
UPDATE e SET
  e.C_FELHASZNALOID = a.ID -- Akt tanéves felhasznaloId javítása köv tanévesre
FROM T_EMAIL e
  INNER JOIN T_TANEV tv ON tv.ID = e.C_TANEVID AND tv.C_KOVETKEZO = 'T'
  INNER JOIN T_ALKALMAZOTT a ON a.ELOZOTANEVIREKORDID = e.C_FELHASZNALOID AND a.C_ALTANEVID = e.C_TANEVID


UPDATE e SET -- Hbásan duplikálódott alapértelmezet emailcímek törlése
   e.TOROLT = 'T'
  ,e.LASTCHANGED = GETDATE()
  ,e.MODIFIER = 0
FROM T_EMAIL e
INNER JOIN (
  SELECT 
     x.ID
    ,ROW_NUMBER() OVER (PARTITION BY x.C_FELHASZNALOID, x.C_GONDVISELOID ORDER BY x.CREATED) RN 
  FROM T_EMAIL x
   INNER JOIN T_TANEV tv ON tv.ID = x.C_TANEVID AND tv.C_KOVETKEZO = 'T'
     AND x.C_ALAPERTELMEZETT = 'T'
  WHERE x.TOROLT = 'F'
) y ON y.ID = e.ID 
WHERE y.RN >= 2

