﻿UPDATE T_TANULOMULASZTAS SET TOROLT='T',MODIFIER=0,LASTCHANGED=GETDATE() WHERE ID IN(
SELECT ID FROM(
SELECT 
	ROW_NUMBER() OVER (PARTITION BY tm.C_TANITASIORAKID,tm.C_ORATANULOIID ORDER BY tm.C_TANITASIORAKID,tm.C_ORATANULOIID ASC, C_IGAZOLT DESC, C_TIPUS ASC) rn, tm.ID FROM(
select COUNT(*) db,C_TANITASIORAKID,C_ORATANULOIID 
from T_TANULOMULASZTAS tm
	INNER JOIN T_TANEV tn ON tn.ID=tm.C_TANEVID AND tn.C_AKTIV='T' AND tn.TOROLT='F'
	INNER JOIN T_INTEZMENY i ON i.ID=tm.C_INTEZMENYID AND i.TOROLT='F' AND i.C_FENNTARTOAZONOSITO<900
WHERE tm.TOROLT='F'
GROUP BY C_TANITASIORAKID,C_ORATANULOIID
HAVING COUNT(*)>1) x 
	INNER JOIN T_TANULOMULASZTAS tm ON tm.C_ORATANULOIID=x.C_ORATANULOIID AND tm.C_TANITASIORAKID=x.C_TANITASIORAKID AND tm.TOROLT='F' ) y
	WHERE y.rn>1)

