﻿-- Elso korben a  duplazasok
UPDATE T_TANULOMULASZTAS SET 
  C_TIPUS = 1817,
  C_KESESPERCBEN = NULL
WHERE ID IN (
  SELECT 
    CASE 
      WHEN t.a_tanoracelu = t.b_tanoracelu AND a_CREATED < b_CREATED THEN b_ID
      WHEN  t.a_tanoracelu = t.b_tanoracelu AND a_CREATED >= b_CREATED THEN a_ID
      WHEN t.a_tanoracelu = 'T' THEN b_ID
      WHEN t.b_tanoracelu = 'T' THEN a_ID 
    END AS IDForUPDATE
  FROM (
    SELECT 
      count(1) over(partition by a.ID) AS a_CNT,count(1) over(partition by b.ID) AS b_CNT,
      a.tanoracelu AS a_tanoracelu,a.ID AS a_ID, a.C_ORAKEZDETE AS a_C_ORAKEZDETE,a.C_ORAVEGE AS a_C_ORAVEGE, a.CREATED AS a_CREATED,
      b.tanoracelu AS b_tanoracelu,b.ID AS b_ID, b.C_ORAKEZDETE AS b_C_ORAKEZDETE,b.C_ORAVEGE AS b_C_ORAVEGE, b.CREATED AS b_CREATED,a.C_ORATANULOIID
    FROM (
      SELECT 
        IIF(o.ID IS NOT NULL OR cs.C_TIPUSA = 1034 OR cst.C_ISTANORAICELU = 'T','T','F') AS tanoracelu, tm.ID,tm.C_INTEZMENYID,ocs.C_NEV,cs.C_TIPUSA,tao.C_OSZTALYCSOPORTID, tao.C_ORAKEZDETE,tao.C_ORAVEGE,tao.C_DATUM, tm.C_ORATANULOIID, tao.ID AS taoID, tm.CREATED
      FROM T_TANULOMULASZTAS tm 
        INNER JOIN T_TANEV tv ON tv.ID = tm.C_TANEVID AND tv.C_AKTIV = 'T'
        INNER JOIN T_TANITASIORA tao ON tm.C_TANITASIORAKID = tao.ID AND tao.TOROLT = 'F'
        INNER JOIN T_OSZTALYCSOPORT ocs ON ocs.ID = tao.C_OSZTALYCSOPORTID AND ocs.TOROLT = 'F'
        LEFT JOIN T_CSOPORT cs ON cs.ID = ocs.ID AND cs.TOROLT = 'F'
        LEFT JOIN T_OSZTALY o ON o.ID = ocs.ID AND o.TOROLT = 'F'
        LEFT JOIN T_CSOPORTTIPUS cst ON cst.ID = cs.C_TIPUSA AND cst.TOROLT = 'F'AND cst.C_ALTANEVID = cs.C_ALTANEVID
      WHERE tm.TOROLT = 'F' 
        AND tm.C_TIPUS BETWEEN 1499 AND 1500 
        AND tao.C_DATUM > '20180831' 
        AND C_IGAZOLASTIPUSA IS NULL
    ) a
      INNER JOIN (
        SELECT 
          IIF(o.ID IS NOT NULL OR cs.C_TIPUSA = 1034 OR cst.C_ISTANORAICELU = 'T','T','F') AS tanoracelu,tm.ID,tm.C_INTEZMENYID,ocs.C_NEV,cs.C_TIPUSA ,tao.C_OSZTALYCSOPORTID,tao.C_ORAKEZDETE,tao.C_ORAVEGE, tao.C_DATUM, tm.C_ORATANULOIID, tao.ID AS taoID, tm.CREATED
        FROM T_TANULOMULASZTAS tm 
          INNER JOIN T_TANEV tv ON tv.ID = tm.C_TANEVID AND tv.C_AKTIV = 'T'
          INNER JOIN T_TANITASIORA tao ON tm.C_TANITASIORAKID = tao.ID AND tao.TOROLT = 'F'
          INNER JOIN T_OSZTALYCSOPORT ocs ON ocs.ID = tao.C_OSZTALYCSOPORTID AND ocs.TOROLT = 'F'
          LEFT JOIN T_CSOPORT cs ON cs.ID = ocs.ID AND cs.TOROLT = 'F'
          LEFT JOIN T_OSZTALY o ON o.ID = ocs.ID AND o.TOROLT = 'F'
          LEFT JOIN T_CSOPORTTIPUS cst ON cst.ID = cs.C_TIPUSA AND cst.TOROLT = 'F'AND cst.C_ALTANEVID = cs.C_ALTANEVID
        WHERE tm.TOROLT = 'F' 
          AND tm.C_TIPUS BETWEEN 1499 AND 1500 
          AND tao.C_DATUM > '20180831' 
          AND C_IGAZOLASTIPUSA IS NULL
    ) b ON a.C_DATUM = b.C_DATUM AND a.C_ORAKEZDETE < b.C_ORAVEGE AND a.C_ORAVEGE > b.C_ORAKEZDETE AND a.C_ORATANULOIID = b.C_ORATANULOIID AND a.CREATED < b.CREATED
  ) t 
  WHERE a_CNT = 1 AND b_CNT = 1
)

-- a tobbszori duplazodasokra 5szor futatni
DECLARE @TanuloID int, @Datum DATETIME, @megtartandoID int
DECLARE @i int = 0

WHILE @i < 5 BEGIN
  DECLARE cur CURSOR LOCAL FOR 
  SELECT DISTINCT a.C_ORATANULOIID, a.C_DATUM
  FROM (
    SELECT 
      IIF(o.ID IS NOT NULL OR cs.C_TIPUSA = 1034 OR cst.C_ISTANORAICELU = 'T','T','F') AS tanoracelu, tm.ID,tm.C_INTEZMENYID,ocs.C_NEV,cs.C_TIPUSA,tao.C_OSZTALYCSOPORTID, tao.C_ORAKEZDETE,tao.C_ORAVEGE,tao.C_DATUM, tm.C_ORATANULOIID, tao.ID AS taoID, tm.CREATED
    FROM T_TANULOMULASZTAS tm 
      INNER JOIN T_TANEV tv ON tv.ID = tm.C_TANEVID AND tv.C_AKTIV = 'T'
      INNER JOIN T_TANITASIORA tao ON tm.C_TANITASIORAKID = tao.ID AND tao.TOROLT = 'F'
      INNER JOIN T_OSZTALYCSOPORT ocs ON ocs.ID = tao.C_OSZTALYCSOPORTID AND ocs.TOROLT = 'F'
      LEFT JOIN T_CSOPORT cs ON cs.ID = ocs.ID AND cs.TOROLT = 'F'
      LEFT JOIN T_OSZTALY o ON o.ID = ocs.ID AND o.TOROLT = 'F'
      LEFT JOIN T_CSOPORTTIPUS cst ON cst.ID = cs.C_TIPUSA AND cst.TOROLT = 'F'AND cst.C_ALTANEVID = cs.C_ALTANEVID
    WHERE tm.TOROLT = 'F' 
      AND tm.C_TIPUS BETWEEN 1499 AND 1500 
      AND tao.C_DATUM > '20180831' 
      AND C_IGAZOLASTIPUSA IS NULL
  ) a
  INNER JOIN (
    SELECT 
      IIF(o.ID IS NOT NULL OR cs.C_TIPUSA = 1034 OR cst.C_ISTANORAICELU = 'T','T','F') AS tanoracelu,tm.ID,tm.C_INTEZMENYID,ocs.C_NEV,cs.C_TIPUSA ,tao.C_OSZTALYCSOPORTID,tao.C_ORAKEZDETE,tao.C_ORAVEGE, tao.C_DATUM, tm.C_ORATANULOIID, tao.ID AS taoID, tm.CREATED
    FROM T_TANULOMULASZTAS tm 
      INNER JOIN T_TANEV tv ON tv.ID = tm.C_TANEVID AND tv.C_AKTIV = 'T'
      INNER JOIN T_TANITASIORA tao ON tm.C_TANITASIORAKID = tao.ID AND tao.TOROLT = 'F'
      INNER JOIN T_OSZTALYCSOPORT ocs ON ocs.ID = tao.C_OSZTALYCSOPORTID AND ocs.TOROLT = 'F'
      LEFT JOIN T_CSOPORT cs ON cs.ID = ocs.ID AND cs.TOROLT = 'F'
      LEFT JOIN T_OSZTALY o ON o.ID = ocs.ID AND o.TOROLT = 'F'
      LEFT JOIN T_CSOPORTTIPUS cst ON cst.ID = cs.C_TIPUSA AND cst.TOROLT = 'F'AND cst.C_ALTANEVID = cs.C_ALTANEVID
    WHERE tm.TOROLT = 'F' 
      AND tm.C_TIPUS BETWEEN 1499 AND 1500 
      AND tao.C_DATUM > '20180831' 
      AND C_IGAZOLASTIPUSA IS NULL
  ) b ON a.C_DATUM = b.C_DATUM 
        AND a.C_ORAKEZDETE < b.C_ORAVEGE AND a.C_ORAVEGE > b.C_ORAKEZDETE 
        AND a.C_ORATANULOIID = b.C_ORATANULOIID AND a.CREATED < b.CREATED 
  OPEN cur; 
  FETCH NEXT FROM cur INTO  @TanuloID, @Datum;
  WHILE @@FETCH_STATUS = 0 BEGIN
  SET @megtartandoID = NULL
  SELECT TOP 1 @megtartandoID = a.ID
  FROM (
    SELECT 
      IIF(o.ID IS NOT NULL OR cs.C_TIPUSA = 1034 OR cst.C_ISTANORAICELU = 'T','T','F') AS tanoracelu, tm.ID,tm.C_INTEZMENYID,ocs.C_NEV,cs.C_TIPUSA,tao.C_OSZTALYCSOPORTID, tao.C_ORAKEZDETE,tao.C_ORAVEGE,tao.C_DATUM, tm.C_ORATANULOIID, tao.ID AS taoID, tm.CREATED
    FROM T_TANULOMULASZTAS tm 
      INNER JOIN T_TANEV tv ON tv.ID = tm.C_TANEVID AND tv.C_AKTIV = 'T'
      INNER JOIN T_TANITASIORA tao ON tm.C_TANITASIORAKID = tao.ID AND tao.TOROLT = 'F'
      INNER JOIN T_OSZTALYCSOPORT ocs ON ocs.ID = tao.C_OSZTALYCSOPORTID AND ocs.TOROLT = 'F'
      LEFT JOIN T_CSOPORT cs ON cs.ID = ocs.ID AND cs.TOROLT = 'F'
      LEFT JOIN T_OSZTALY o ON o.ID = ocs.ID AND o.TOROLT = 'F'
      LEFT JOIN T_CSOPORTTIPUS cst ON cst.ID = cs.C_TIPUSA AND cst.TOROLT = 'F'AND cst.C_ALTANEVID = cs.C_ALTANEVID
    WHERE tm.TOROLT = 'F' 
      AND tm.C_TIPUS BETWEEN 1499 AND 1500 
      AND tao.C_DATUM > '20180831' 
      AND C_IGAZOLASTIPUSA IS NULL
      AND tm.C_ORATANULOIID = @TanuloID 
      AND tao.C_DATUM = @Datum
  ) a
  INNER JOIN (
    SELECT 
      IIF(o.ID IS NOT NULL OR cs.C_TIPUSA = 1034 OR cst.C_ISTANORAICELU = 'T','T','F') AS tanoracelu,tm.ID,tm.C_INTEZMENYID,ocs.C_NEV,cs.C_TIPUSA ,tao.C_OSZTALYCSOPORTID,tao.C_ORAKEZDETE,tao.C_ORAVEGE, tao.C_DATUM, tm.C_ORATANULOIID, tao.ID AS taoID, tm.CREATED
    FROM T_TANULOMULASZTAS tm 
      INNER JOIN T_TANEV tv ON tv.ID = tm.C_TANEVID AND tv.C_AKTIV = 'T'
      INNER JOIN T_TANITASIORA tao ON tm.C_TANITASIORAKID = tao.ID AND tao.TOROLT = 'F'
      INNER JOIN T_OSZTALYCSOPORT ocs ON ocs.ID = tao.C_OSZTALYCSOPORTID AND ocs.TOROLT = 'F'
      LEFT JOIN T_CSOPORT cs ON cs.ID = ocs.ID AND cs.TOROLT = 'F'
      LEFT JOIN T_OSZTALY o ON o.ID = ocs.ID AND o.TOROLT = 'F'
      LEFT JOIN T_CSOPORTTIPUS cst ON cst.ID = cs.C_TIPUSA AND cst.TOROLT = 'F'AND cst.C_ALTANEVID = cs.C_ALTANEVID
    WHERE tm.TOROLT = 'F' 
      AND tm.C_TIPUS BETWEEN 1499 AND 1500 
      AND tao.C_DATUM > '20180831' 
      AND C_IGAZOLASTIPUSA IS NULL
      AND tm.C_ORATANULOIID = @TanuloID 
      AND tao.C_DATUM = @Datum
  ) b ON a.C_DATUM = b.C_DATUM AND
         a.C_ORAKEZDETE < b.C_ORAVEGE AND a.C_ORAVEGE > b.C_ORAKEZDETE 
         AND a.C_ORATANULOIID = b.C_ORATANULOIID AND a.CREATED < b.CREATED
  ORDER BY a.tanoracelu DESC,a.CREATED
  UPDATE T_TANULOMULASZTAS SET 
    C_TIPUS = 1817,
    C_KESESPERCBEN = NULL
  WHERE ID IN(
    SELECT b.ID
    FROM (
      SELECT 
        IIF(o.ID IS NOT NULL OR cs.C_TIPUSA = 1034 OR cst.C_ISTANORAICELU = 'T','T','F') AS tanoracelu, tm.ID,tm.C_INTEZMENYID,ocs.C_NEV,cs.C_TIPUSA,tao.C_OSZTALYCSOPORTID, tao.C_ORAKEZDETE,tao.C_ORAVEGE,tao.C_DATUM, tm.C_ORATANULOIID, tao.ID AS taoID, tm.CREATED
      FROM T_TANULOMULASZTAS tm 
        INNER JOIN T_TANEV tv ON tv.ID = tm.C_TANEVID AND tv.C_AKTIV = 'T'
        INNER JOIN T_TANITASIORA tao ON tm.C_TANITASIORAKID = tao.ID AND tao.TOROLT = 'F'
        INNER JOIN T_OSZTALYCSOPORT ocs ON ocs.ID = tao.C_OSZTALYCSOPORTID AND ocs.TOROLT = 'F'
        LEFT JOIN T_CSOPORT cs ON cs.ID = ocs.ID AND cs.TOROLT = 'F'
        LEFT JOIN T_OSZTALY o ON o.ID = ocs.ID AND o.TOROLT = 'F'
        LEFT JOIN T_CSOPORTTIPUS cst ON cst.ID = cs.C_TIPUSA AND cst.TOROLT = 'F'AND cst.C_ALTANEVID = cs.C_ALTANEVID
      WHERE tm.TOROLT = 'F' 
        AND tm.C_TIPUS BETWEEN 1499 AND 1500 
        AND tao.C_DATUM > '20180831' 
        AND C_IGAZOLASTIPUSA IS NULL
        AND tm.C_ORATANULOIID = @TanuloID 
        AND tao.C_DATUM = @Datum 
        AND tm.ID = @megtartandoID
    ) a
    INNER JOIN (
      SELECT 
        IIF(o.ID IS NOT NULL OR cs.C_TIPUSA = 1034 OR cst.C_ISTANORAICELU = 'T','T','F') AS tanoracelu,tm.ID,tm.C_INTEZMENYID,ocs.C_NEV,cs.C_TIPUSA ,tao.C_OSZTALYCSOPORTID,tao.C_ORAKEZDETE,tao.C_ORAVEGE, tao.C_DATUM, tm.C_ORATANULOIID, tao.ID AS taoID, tm.CREATED
      FROM T_TANULOMULASZTAS tm 
        INNER JOIN T_TANEV tv ON tv.ID = tm.C_TANEVID AND tv.C_AKTIV = 'T'
        INNER JOIN T_TANITASIORA tao ON tm.C_TANITASIORAKID = tao.ID AND tao.TOROLT = 'F'
        INNER JOIN T_OSZTALYCSOPORT ocs ON ocs.ID = tao.C_OSZTALYCSOPORTID AND ocs.TOROLT = 'F'
        LEFT JOIN T_CSOPORT cs ON cs.ID = ocs.ID AND cs.TOROLT = 'F'
        LEFT JOIN T_OSZTALY o ON o.ID = ocs.ID AND o.TOROLT = 'F'
        LEFT JOIN T_CSOPORTTIPUS cst ON cst.ID = cs.C_TIPUSA AND cst.TOROLT = 'F'AND cst.C_ALTANEVID = cs.C_ALTANEVID
      WHERE tm.TOROLT = 'F' 
        AND tm.C_TIPUS BETWEEN 1499 AND 1500 
        AND tao.C_DATUM > '20180831' 
        AND C_IGAZOLASTIPUSA IS NULL
        AND tm.C_ORATANULOIID = @TanuloID 
        AND tao.C_DATUM = @Datum
    ) b ON a.C_DATUM = b.C_DATUM 
          AND a.C_ORAKEZDETE < b.C_ORAVEGE AND a.C_ORAVEGE > b.C_ORAKEZDETE 
          AND a.C_ORATANULOIID = b.C_ORATANULOIID AND a.CREATED < b.CREATED
    )
  FETCH NEXT FROM cur INTO  @TanuloID, @Datum;
  END;
  SET @i+=1
  CLOSE cur;
  DEALLOCATE cur;
END;