﻿UPDATE T_TANEVRENDJE SET TOROLT='T', LASTCHANGED = GETDATE(),MODIFIER= 0
WHERE ID IN(
  SELECT ID FROM(
    SELECT 
       tr.ID
      ,ROW_NUMBER() OVER (PARTITION BY tr.C_DATUM,tr.C_TANEVID ORDER BY tr.CREATED) rn 
    FROM T_TANEVRENDJE tr
      INNER JOIN
        (SELECT 
           C_DATUM
          ,C_TANEVID 
        FROM T_TANEVRENDJE
        WHERE TOROLT='F' AND C_OSSZESCSOPORTRAVONATKOZIK = 'T'
        GROUP BY C_DATUM,C_TANEVID
        HAVING COUNT(*)>1) x ON x.C_DATUM = tr.C_DATUM AND x.C_TANEVID = tr.C_TANEVID
    WHERE tr.TOROLT='F') x WHERE x.rn>1)

GO
DROP INDEX IF EXISTS T_TANEVRENDJE.NCU_TanevRendje_Datum_TanevId_IntezmenyId
CREATE UNIQUE INDEX NCU_TanevRendje_Datum_TanevId_IntezmenyId
ON T_TANEVRENDJE(
     C_DATUM
    ,C_TANEVID
    ,C_INTEZMENYID
  )
WHERE TOROLT='F' AND C_OSSZESCSOPORTRAVONATKOZIK='T'
WITH (FILLFACTOR = 80)