﻿SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

DROP PROCEDURE IF EXISTS [dbo].[sp_GetMunkaidoElszamoloLap]
GO

CREATE PROCEDURE [dbo].[sp_GetMunkaidoElszamoloLap]
	@pIntezmenyId	INT,
	@pTanarId		INT,
	@tanevId		INT,
	@honapId		INT,
	@isKlebersberg	BIT,
  @feladatKategoriaId INT = NULL
AS
  BEGIN
	 SET NOCOUNT ON;

  DECLARE @backDate datetime

	SELECT
		ID      pedagogusId
		,Nev    pedagogusNev
	FROM  fnGetDokumentumIntezmenyPedagogusok (@pIntezmenyId, @tanevId )
	WHERE ID = @pTanarId


	DECLARE @Honap INT = (
		SELECT TOP 1
			dictionaryItemBase.C_VALUE
		FROM
			T_DICTIONARYITEMBASE_OSSZES dictionaryItemBase
		WHERE
			dictionaryItemBase.ID = @honapId
			AND dictionaryItemBase.C_TANEVID = @tanevId
			AND dictionaryItemBase.TOROLT = 'F'
	)

  CREATE TABLE #EvHonap (Ev int, Honap int)
  INSERT INTO #EvHonap
	SELECT TOP 1
		FORMAT(C_NAPDATUMA, 'yyyy')		Ev,
		FORMAT(C_NAPDATUMA, 'MM')		Honap
	FROM
		T_NAPTARINAP_OSSZES naptariNap
	WHERE
		DATEPART(MONTH,naptariNap.C_NAPDATUMA) = @Honap
		AND naptariNap.C_TANEVID = @tanevId
		AND naptariNap.TOROLT = 'F'
  SELECT * FROM #EvHonap

  SET @backDate = DATEADD(MONTH, 1, DATEFROMPARTS((SELECT Ev FROM #EvHonap), (SELECT Honap FROM #EvHonap), 1))

--===================================
--FEJLÉC
--===================================
	SELECT
		f.ID									    pedagogusId
		,f.C_NYOMTATASINEV							TanarNev
		,f.C_OKTATASIAZONOSITO						OktatasiAzonosito
		,munkakor.C_NAME							Munkakor
		,dbo.fnGetDokumentumPedagogusOraszam(f.id, @isKlebersberg) 	KotelezoOraszam
		,ISNULL(CONVERT(NVARCHAR(MAX),m.C_MUNKAIDOKEDVEZMENYORASZAM),'-')	MunkaidoKedvezmenyOraszam
		,ISNULL(munkaidoKedvezmenyOka.C_NAME,'-')	MunkaidoKedvezmenyOka
		,ia.C_NEV + ' (' + ia.C_OMKOD +')'			IntezmenyNeve
		,ia.C_IGAZGATONEVE							Igazgato
    ,m.C_FELADATELLATASIHELYID AS FeladatEllatasiHelyId
	FROM T_FELHASZNALO_OSSZES f
		INNER JOIN T_MUNKAUGYIADATOK_OSSZES m ON m.C_ALKALMAZOTTID = f.Id
		LEFT JOIN T_DICTIONARYITEMBASE_OSSZES munkakor ON munkakor.Id = m.C_MUNKAKORTIPUSA AND munkakor.C_TANEVID = m.C_TANEVID
		LEFT JOIN T_DICTIONARYITEMBASE_OSSZES munkaidoKedvezmenyOka ON munkaidoKedvezmenyOka.Id = m.C_MUNKAIDOKEDVEZMENYOKA AND munkaidoKedvezmenyOka.C_TANEVID = m.C_TANEVID
		INNER JOIN T_INTEZMENYADATOK_OSSZES ia ON ia.C_TANEVID = f.C_TANEVID
	WHERE
			ia.TOROLT = 'F'
		AND m.TOROLT = 'F'
		AND f.ID = @pTanarId

--===================================
--MEGTARTOTT TANÓRÁK, HELYETTESÍTÉSEK
--===================================
	SELECT
		pedagogusId
		,OsztalyCsoportID
		,OsztalyCsoportNev
		,Honap
		,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
		 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
		 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31],
		 ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) +
		 ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) + ISNULL([9], 0) + ISNULL([10], 0) +
		 ISNULL([11], 0) + ISNULL([12], 0) + ISNULL([13], 0) + ISNULL([14], 0) + ISNULL([15], 0) +
		 ISNULL([16], 0) + ISNULL([17], 0) + ISNULL([18], 0) + ISNULL([19], 0) + ISNULL([20], 0) +
		 ISNULL([21], 0) + ISNULL([22], 0) + ISNULL([23], 0) + ISNULL([24], 0) + ISNULL([25], 0) +
		 ISNULL([26], 0) + ISNULL([27], 0) + ISNULL([28], 0) + ISNULL([29], 0) + ISNULL([30], 0) + ISNULL([31], 0) Ossz
     INTO #MegtartottOrakHelyesitesek_tmp
	FROM
	(
	SELECT
		T_TANITASIORA_OSSZES.C_ORATULAJDONOSID   AS		pedagogusId
		,DATEPART(DAY, T_TANITASIORA_OSSZES.C_DATUM)	Nap
		,DATEPART(Month, T_TANITASIORA_OSSZES.C_DATUM)	Honap
		,T_OSZTALYCSOPORT_OSSZES.ID						OsztalyCsoportID
		,T_OSZTALYCSOPORT_OSSZES.ID						ocsId
		,T_OSZTALYCSOPORT_OSSZES.C_NEV					OsztalyCsoportNev
		,IIF(T_TANITASIORA_OSSZES.C_CSENGETESIRENDORAID IS NOT NULL,
				1,
				ROUND(CONVERT(FLOAT,(DATEPART(HOUR, C_ORAVEGE - C_ORAKEZDETE) * 60 + DATEPART(MINUTE, C_ORAVEGE - C_ORAKEZDETE))) / 45, 2)
			)SumOra
	FROM T_TANITASIORA_OSSZES
		INNER JOIN T_OSZTALYCSOPORT_OSSZES on T_TANITASIORA_OSSZES.C_OSZTALYCSOPORTID = T_OSZTALYCSOPORT_OSSZES.ID AND (T_OSZTALYCSOPORT_OSSZES.C_FELADATKATEGORIAID = @feladatKategoriaId OR @feladatKategoriaId IS NULL)
		LEFT JOIN T_CSOPORT_OSSZES ON T_CSOPORT_OSSZES.Id = T_OSZTALYCSOPORT_OSSZES.ID
		LEFT JOIN T_FOGLALKOZAS_OSSZES fog ON fog.ID = C_FOGLALKOZASID
	WHERE
			T_TANITASIORA_OSSZES.C_TANEVID = @tanevId
		AND T_TANITASIORA_OSSZES.C_ORATULAJDONOSID = @pTanarId
		AND DATEPART(MONTH, T_TANITASIORA_OSSZES.C_DATUM) = @Honap
		AND T_TANITASIORA_OSSZES.TOROLT = 'F'
		AND T_TANITASIORA_OSSZES.C_MEGTARTOTT = 'T'
		AND T_TANITASIORA_OSSZES.C_HELYETTESITOTANARID IS NULL
		AND (T_CSOPORT_OSSZES.Id IS NULL OR T_CSOPORT_OSSZES.C_TIPUSA IN (SELECT ID FROM fnGetTanoraiCeluCsoportTipusok(@tanevId)))
		AND (T_TANITASIORA_OSSZES.C_PARHUZAMOSORA = 'F' OR T_TANITASIORA_OSSZES.C_PARHUZAMOSORA IS NULL)
		AND (fog.C_FOGLALKOZASTIPUSA IS NULL OR fog.C_FOGLALKOZASTIPUSA <> 1371)
	)a
	PIVOT
	(
	SUM(SumOra)
	FOR Nap IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])
	) OsszTanora

  ----ITT ADOM VISSZA A TABLAT=======================================<<<<<<
  SELECT * FROM #MegtartottOrakHelyesitesek_tmp

  SELECT
	  pedagogusId
	  ,SUM([1]) AS [1], SUM([2]) AS [2], SUM([3]) AS [3], SUM([4]) AS [4], SUM([5]) AS [5], SUM([6]) AS [6], SUM([7]) AS [7], SUM([8]) AS [8], SUM([9]) AS [9], SUM([10]) AS [10],
	  SUM([11]) AS [11], SUM([12]) AS [12], SUM([13]) AS [13], SUM([14]) AS [14], SUM([15]) AS [15], SUM([16]) AS [16], SUM([17]) AS [17], SUM([18]) AS [18], SUM([19]) AS [19], SUM([20]) AS [20],
	  SUM([21]) AS [21], SUM([22]) AS [22], SUM([23]) AS [23], SUM([24]) AS [24], SUM([25]) AS [25], SUM([26]) AS [26], SUM([27]) AS [27], SUM([28]) AS [28], SUM([29]) AS [29], SUM([30]) AS [30], SUM([31]) AS [31]
	  ,SUM(Ossz) as Ossz
 FROM #MegtartottOrakHelyesitesek_tmp
 GROUP BY
	pedagogusId

--szakszerű helyettesítés
	SELECT
		pedagogusId
		,OsztalyCsoportID
		,OsztalyCsoportNev
		,Honap
		,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
		 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
		 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31],
		 ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) +
		 ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) + ISNULL([9], 0) + ISNULL([10], 0) +
		 ISNULL([11], 0) + ISNULL([12], 0) + ISNULL([13], 0) + ISNULL([14], 0) + ISNULL([15], 0) +
		 ISNULL([16], 0) + ISNULL([17], 0) + ISNULL([18], 0) + ISNULL([19], 0) + ISNULL([20], 0) +
		 ISNULL([21], 0) + ISNULL([22], 0) + ISNULL([23], 0) + ISNULL([24], 0) + ISNULL([25], 0) +
		 ISNULL([26], 0) + ISNULL([27], 0) + ISNULL([28], 0) + ISNULL([29], 0) + ISNULL([30], 0) + ISNULL([31], 0) Ossz
     INTO #SzakszeruHelyetesites_tmp
	FROM
	(
	SELECT
		tao.C_HELYETTESITOTANARID   AS  pedagogusId
		,DATEPART(DAY, tao.C_DATUM)		Nap
		,DATEPART(MONTH,tao.C_DATUM)	Honap
		,ocs.C_NEV						OsztalyCsoportNev
		,tao.C_OSZTALYCSOPORTID			oraid
		,tao.C_OSZTALYCSOPORTID			OsztalyCsoportID
		,IIF(tao.C_CSENGETESIRENDORAID IS NOT NULL,
				1,
				ROUND(CONVERT(FLOAT,(DATEPART(HOUR, C_ORAVEGE - C_ORAKEZDETE) * 60 + DATEPART(MINUTE, C_ORAVEGE - C_ORAKEZDETE))) / IIF(cs.Id IS NULL OR cs.C_TIPUSA IN (SELECT ID FROM fnGetTanoraiCeluCsoportTipusok(@tanevId)), 45, 60), 2)
			)SumOra
	FROM T_TANITASIORA_OSSZES tao
		INNER JOIN T_OSZTALYCSOPORT_OSSZES ocs on ocs.id=tao.C_OSZTALYCSOPORTID AND (ocs.C_FELADATKATEGORIAID = @feladatKategoriaId OR @feladatKategoriaId IS NULL)
		LEFT JOIN T_CSOPORT_OSSZES  cs ON cs.ID = ocs.ID
	WHERE
			DATEPART(MONTH, tao.C_DATUM) = @Honap
		AND tao.C_HELYETTESITOTANARID = @pTanarId
		AND tao.TOROLT = 'F'
		AND tao.C_HELYETTESITESTIPUSA = 1494
		AND tao.C_MEGTARTOTT = 'T'
		AND tao.C_TANEVID = @tanevId
		AND (tao.C_PARHUZAMOSORA = 'F' OR tao.C_PARHUZAMOSORA IS NULL)
	)a
	PIVOT
	(
	SUM(SumOra)
	FOR Nap IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])

	) SzakszeruHelyettesites

  ----ITT ADOM VISSZA A TABLAT=======================================<<<<<<
  SELECT * FROM #SzakszeruHelyetesites_tmp

  SELECT
	pedagogusId
	,SUM([1]) AS [1], SUM([2]) AS [2], SUM([3]) AS [3], SUM([4]) AS [4], SUM([5]) AS [5], SUM([6]) AS [6], SUM([7]) AS [7], SUM([8]) AS [8], SUM([9]) AS [9], SUM([10]) AS [10],
	SUM([11]) AS [11], SUM([12]) AS [12], SUM([13]) AS [13], SUM([14]) AS [14], SUM([15]) AS [15], SUM([16]) AS [16], SUM([17]) AS [17], SUM([18]) AS [18], SUM([19]) AS [19], SUM([20]) AS [20],
	SUM([21]) AS [21], SUM([22]) AS [22], SUM([23]) AS [23], SUM([24]) AS [24], SUM([25]) AS [25], SUM([26]) AS [26], SUM([27]) AS [27], SUM([28]) AS [28], SUM([29]) AS [29], SUM([30]) AS [30], SUM([31]) AS [31]
  	,SUM(Ossz) as Ossz
  FROM #SzakszeruHelyetesites_tmp
  GROUP BY
	pedagogusId

--nem szakszerű helyettesítés
	SELECT
		pedagogusId
		,OsztalyCsoportID
		,OsztalyCsoportNev
		,Honap
		,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
		 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
		 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31],
		 ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) +
		 ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) + ISNULL([9], 0) + ISNULL([10], 0) +
		 ISNULL([11], 0) + ISNULL([12], 0) + ISNULL([13], 0) + ISNULL([14], 0) + ISNULL([15], 0) +
		 ISNULL([16], 0) + ISNULL([17], 0) + ISNULL([18], 0) + ISNULL([19], 0) + ISNULL([20], 0) +
		 ISNULL([21], 0) + ISNULL([22], 0) + ISNULL([23], 0) + ISNULL([24], 0) + ISNULL([25], 0) +
		 ISNULL([26], 0) + ISNULL([27], 0) + ISNULL([28], 0) + ISNULL([29], 0) + ISNULL([30], 0) + ISNULL([31], 0) Ossz
     INTO #NemSzakszeruHelyetesites_tmp
	FROM
	(
	SELECT
		tao.C_HELYETTESITOTANARID   AS  pedagogusId
		,DATEPART(DAY, tao.C_DATUM)		Nap
		,DATEPART(MONTH,tao.C_DATUM)	Honap
		,ocs.C_NEV						OsztalyCsoportNev
		,tao.C_OSZTALYCSOPORTID			oraid
		,tao.C_OSZTALYCSOPORTID			OsztalyCsoportID
		,IIF(tao.C_CSENGETESIRENDORAID IS NOT NULL,
				1,
				ROUND(CONVERT(FLOAT,(DATEPART(HOUR, C_ORAVEGE - C_ORAKEZDETE) * 60 + DATEPART(MINUTE, C_ORAVEGE - C_ORAKEZDETE))) / IIF(cs.Id IS NULL OR cs.C_TIPUSA IN (SELECT ID FROM fnGetTanoraiCeluCsoportTipusok(@tanevId)), 45, 60), 2)
		)SumOra
	FROM T_TANITASIORA_OSSZES tao
		INNER JOIN T_OSZTALYCSOPORT_OSSZES ocs on ocs.id=tao.C_OSZTALYCSOPORTID AND (ocs.C_FELADATKATEGORIAID = @feladatKategoriaId OR @feladatKategoriaId IS NULL)
		LEFT JOIN T_CSOPORT_OSSZES cs ON cs.ID = ocs.ID
	WHERE
			DATEPART(MONTH, tao.C_DATUM) = @Honap
		AND tao.C_HELYETTESITOTANARID = @pTanarId
		AND tao.TOROLT = 'F'
		AND tao.C_HELYETTESITESTIPUSA = 1495
		AND tao.C_MEGTARTOTT = 'T'
		AND tao.C_TANEVID = @tanevId
		AND (tao.C_PARHUZAMOSORA = 'F' OR tao.C_PARHUZAMOSORA IS NULL)
	)a
	PIVOT
	(
	SUM(SumOra)
	FOR Nap IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])

	) NemSzakszeruHelyettesites

  ----ITT ADOM VISSZA A TABLAT=======================================<<<<<<
  SELECT * FROM #NemSzakszeruHelyetesites_tmp

  SELECT
  	 pedagogusId
	,SUM([1]) AS [1], SUM([2]) AS [2], SUM([3]) AS [3], SUM([4]) AS [4], SUM([5]) AS [5], SUM([6]) AS [6], SUM([7]) AS [7], SUM([8]) AS [8], SUM([9]) AS [9], SUM([10]) AS [10],
	SUM([11]) AS [11], SUM([12]) AS [12], SUM([13]) AS [13], SUM([14]) AS [14], SUM([15]) AS [15], SUM([16]) AS [16], SUM([17]) AS [17], SUM([18]) AS [18], SUM([19]) AS [19], SUM([20]) AS [20],
	SUM([21]) AS [21], SUM([22]) AS [22], SUM([23]) AS [23], SUM([24]) AS [24], SUM([25]) AS [25], SUM([26]) AS [26], SUM([27]) AS [27], SUM([28]) AS [28], SUM([29]) AS [29], SUM([30]) AS [30], SUM([31]) AS [31]
  	,SUM(Ossz) as Ossz
  FROM #NemSzakszeruHelyetesites_tmp
  GROUP BY
	 pedagogusId

--óraösszevonás
	SELECT
		pedagogusId
		,OsztalyCsoportID
		,OsztalyCsoportNev
		,Honap
		,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
		 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
		 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31],
		 ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) +
		 ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) + ISNULL([9], 0) + ISNULL([10], 0) +
		 ISNULL([11], 0) + ISNULL([12], 0) + ISNULL([13], 0) + ISNULL([14], 0) + ISNULL([15], 0) +
		 ISNULL([16], 0) + ISNULL([17], 0) + ISNULL([18], 0) + ISNULL([19], 0) + ISNULL([20], 0) +
		 ISNULL([21], 0) + ISNULL([22], 0) + ISNULL([23], 0) + ISNULL([24], 0) + ISNULL([25], 0) +
		 ISNULL([26], 0) + ISNULL([27], 0) + ISNULL([28], 0) + ISNULL([29], 0) + ISNULL([30], 0) + ISNULL([31], 0) Ossz
     INTO #OraOsszevonas_tmp
	FROM
	(
	SELECT
		tao.C_HELYETTESITOTANARID   AS  pedagogusId
		,DATEPART(DAY, tao.C_DATUM)	Nap
		,DATEPART(MONTH,tao.C_DATUM) Honap
		,ocs.C_NEV					OsztalyCsoportNev
		,tao.C_OSZTALYCSOPORTID		oraid
		,tao.C_OSZTALYCSOPORTID		OsztalyCsoportID
		,IIF(tao.C_CSENGETESIRENDORAID IS NOT NULL,
				1,
				ROUND(CONVERT(FLOAT,(DATEPART(HOUR, C_ORAVEGE - C_ORAKEZDETE) * 60 + DATEPART(MINUTE, C_ORAVEGE - C_ORAKEZDETE))) / IIF(cs.Id IS NULL OR cs.C_TIPUSA IN (SELECT ID FROM fnGetTanoraiCeluCsoportTipusok(@tanevId)), 45, 60), 2)
		)SumOra
	FROM T_TANITASIORA_OSSZES tao
		INNER JOIN T_OSZTALYCSOPORT_OSSZES ocs on ocs.ID=tao.C_OSZTALYCSOPORTID AND (ocs.C_FELADATKATEGORIAID = @feladatKategoriaId OR @feladatKategoriaId IS NULL)
		LEFT JOIN T_CSOPORT_OSSZES cs ON cs.ID = ocs.ID
	WHERE
			DATEPART(MONTH, tao.C_DATUM) = @Honap
		AND tao.C_HELYETTESITOTANARID = @pTanarId
		AND tao.TOROLT = 'F'
		AND tao.C_HELYETTESITESTIPUSA = 1496
		AND tao.C_TANEVID = @tanevId
		AND (tao.C_PARHUZAMOSORA = 'F' OR tao.C_PARHUZAMOSORA IS NULL)
	)a
	PIVOT
	(
	SUM(SumOra)
	FOR Nap IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])

	) Oraosszevonas

  ----ITT ADOM VISSZA A TABLAT=======================================<<<<<<
  SELECT * FROM #OraOsszevonas_tmp

  SELECT
  	pedagogusId
	,SUM([1]) AS [1], SUM([2]) AS [2], SUM([3]) AS [3], SUM([4]) AS [4], SUM([5]) AS [5], SUM([6]) AS [6], SUM([7]) AS [7], SUM([8]) AS [8], SUM([9]) AS [9], SUM([10]) AS [10],
	SUM([11]) AS [11], SUM([12]) AS [12], SUM([13]) AS [13], SUM([14]) AS [14], SUM([15]) AS [15], SUM([16]) AS [16], SUM([17]) AS [17], SUM([18]) AS [18], SUM([19]) AS [19], SUM([20]) AS [20],
	SUM([21]) AS [21], SUM([22]) AS [22], SUM([23]) AS [23], SUM([24]) AS [24], SUM([25]) AS [25], SUM([26]) AS [26], SUM([27]) AS [27], SUM([28]) AS [28], SUM([29]) AS [29], SUM([30]) AS [30], SUM([31]) AS [31]
  	,SUM(Ossz) as Ossz
  FROM #OraOsszevonas_tmp
  GROUP BY
	pedagogusId


--tanóránkívüli
	SELECT
		pedagogusId
		,OsztalyCsoportID
		,OsztalyCsoportNev
		,Honap
		,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
		 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
		 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31],
		 ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) +
		 ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) + ISNULL([9], 0) + ISNULL([10], 0) +
		 ISNULL([11], 0) + ISNULL([12], 0) + ISNULL([13], 0) + ISNULL([14], 0) + ISNULL([15], 0) +
		 ISNULL([16], 0) + ISNULL([17], 0) + ISNULL([18], 0) + ISNULL([19], 0) + ISNULL([20], 0) +
		 ISNULL([21], 0) + ISNULL([22], 0) + ISNULL([23], 0) + ISNULL([24], 0) + ISNULL([25], 0) +
		 ISNULL([26], 0) + ISNULL([27], 0) + ISNULL([28], 0) + ISNULL([29], 0) + ISNULL([30], 0) + ISNULL([31], 0) Ossz
     INTO #TanoranKivuli_tmp
	FROM
	(
	SELECT
		tao.C_ORATULAJDONOSID   AS		pedagogusId
		,DATEPART(DAY, tao.C_DATUM)		Nap
		,DATEPART(MONTH,tao.C_DATUM)	Honap
		,ocs.C_NEV						OsztalyCsoportNev
		,tao.C_OSZTALYCSOPORTID			oraid
		,tao.C_OSZTALYCSOPORTID			OsztalyCsoportID
		,IIF(tao.C_CSENGETESIRENDORAID IS NOT NULL,
				1,
				ROUND(CONVERT(FLOAT,(DATEPART(HOUR, C_ORAVEGE - C_ORAKEZDETE) * 60 + DATEPART(MINUTE, C_ORAVEGE - C_ORAKEZDETE))) / 60, 2)
		)SumOra
	FROM T_TANITASIORA_OSSZES tao
		INNER JOIN T_OSZTALYCSOPORT_OSSZES ocs on ocs.id=tao.C_OSZTALYCSOPORTID AND (ocs.C_FELADATKATEGORIAID = @feladatKategoriaId OR @feladatKategoriaId IS NULL)
		LEFT JOIN T_FOGLALKOZAS_OSSZES fog on fog.id=tao.C_FOGLALKOZASID
		INNER JOIN T_CSOPORT_OSSZES ON T_CSOPORT_OSSZES.Id = ocs.ID
	WHERE
			DATEPART(MONTH, tao.C_DATUM) = @Honap
		AND tao.C_ORATULAJDONOSID = @pTanarId
		AND tao.C_HELYETTESITOTANARID IS NULL
		AND tao.TOROLT = 'F'
		AND tao.C_TANEVID = @tanevId
		AND T_CSOPORT_OSSZES.C_TIPUSA NOT IN (SELECT ID FROM fnGetTanoraiCeluCsoportTipusok(@tanevId))
		AND tao.C_MEGTARTOTT = 'T'
		AND (tao.C_PARHUZAMOSORA = 'F' OR tao.C_PARHUZAMOSORA IS NULL)
		AND (fog.C_FOGLALKOZASTIPUSA IS NULL OR fog.C_FOGLALKOZASTIPUSA <> 1371)
	)a
	PIVOT
	(
	SUM(SumOra)
	FOR Nap IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])

	) TanoranKivuli

  ----ITT ADOM VISSZA A TABLAT=======================================<<<<<<
  SELECT * FROM	#TanoranKivuli_tmp

  SELECT
  	pedagogusId
	,SUM([1]) AS [1], SUM([2]) AS [2], SUM([3]) AS [3], SUM([4]) AS [4], SUM([5]) AS [5], SUM([6]) AS [6], SUM([7]) AS [7], SUM([8]) AS [8], SUM([9]) AS [9], SUM([10]) AS [10],
	SUM([11]) AS [11], SUM([12]) AS [12], SUM([13]) AS [13], SUM([14]) AS [14], SUM([15]) AS [15], SUM([16]) AS [16], SUM([17]) AS [17], SUM([18]) AS [18], SUM([19]) AS [19], SUM([20]) AS [20],
	SUM([21]) AS [21], SUM([22]) AS [22], SUM([23]) AS [23], SUM([24]) AS [24], SUM([25]) AS [25], SUM([26]) AS [26], SUM([27]) AS [27], SUM([28]) AS [28], SUM([29]) AS [29], SUM([30]) AS [30], SUM([31]) AS [31]
  	,SUM(Ossz) as Ossz
  FROM #TanoranKivuli_tmp
  GROUP BY
	 pedagogusId

--egyéni foglalkozas
	SELECT
		pedagogusId
		,OsztalyCsoportID
		,OsztalyCsoportNev
		,Honap
		,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
		 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
		 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31],
		 ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) +
		 ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) + ISNULL([9], 0) + ISNULL([10], 0) +
		 ISNULL([11], 0) + ISNULL([12], 0) + ISNULL([13], 0) + ISNULL([14], 0) + ISNULL([15], 0) +
		 ISNULL([16], 0) + ISNULL([17], 0) + ISNULL([18], 0) + ISNULL([19], 0) + ISNULL([20], 0) +
		 ISNULL([21], 0) + ISNULL([22], 0) + ISNULL([23], 0) + ISNULL([24], 0) + ISNULL([25], 0) +
		 ISNULL([26], 0) + ISNULL([27], 0) + ISNULL([28], 0) + ISNULL([29], 0) + ISNULL([30], 0) + ISNULL([31], 0) Ossz
		INTO #EgyeniFoglalkozas_tmp
  FROM
	(
	SELECT
		tao.C_ORATULAJDONOSID   AS  pedagogusId
		,DATEPART(DAY, tao.C_DATUM)	Nap,
		DATEPART(MONTH,tao.C_DATUM) Honap,
		ocs.C_NEV					OsztalyCsoportNev,
		tao.C_OSZTALYCSOPORTID		oraid,
		tao.C_OSZTALYCSOPORTID		OsztalyCsoportID
		,IIF(tao.C_CSENGETESIRENDORAID IS NOT NULL,
				1,
				ROUND(CONVERT(FLOAT,(DATEPART(HOUR, C_ORAVEGE - C_ORAKEZDETE) * 60 + DATEPART(MINUTE, C_ORAVEGE - C_ORAKEZDETE))) / IIF(cs.Id IS NULL OR cs.C_TIPUSA IN (SELECT ID FROM fnGetTanoraiCeluCsoportTipusok(@tanevId)), 45, 60), 2)
		)SumOra
	FROM T_TANITASIORA_OSSZES tao
		INNER JOIN T_OSZTALYCSOPORT_OSSZES ocs on ocs.id=tao.C_OSZTALYCSOPORTID AND (ocs.C_FELADATKATEGORIAID = @feladatKategoriaId OR @feladatKategoriaId IS NULL)
		LEFT JOIN T_CSOPORT_OSSZES cs ON cs.Id = ocs.Id
		INNER JOIN T_FOGLALKOZAS_OSSZES fog on fog.id=tao.C_FOGLALKOZASID
	WHERE
				DATEPART(MONTH, tao.C_DATUM) = @Honap
			AND tao.C_ORATULAJDONOSID = @pTanarId
			AND fog.C_FOGLALKOZASTIPUSA = 1371
			AND tao.C_HELYETTESITOTANARID IS NULL
			AND tao.TOROLT = 'F'
			AND tao.C_TANEVID = @tanevId
			AND tao.C_MEGTARTOTT = 'T'
			AND (tao.C_PARHUZAMOSORA = 'F' OR tao.C_PARHUZAMOSORA IS NULL)
	)a
	PIVOT
	(
	SUM(SumOra)
	FOR Nap IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])

	) EgyeniFoglalkozas

	  ----ITT ADOM VISSZA A TABLAT=======================================<<<<<<

  SELECT * FROM #EgyeniFoglalkozas_tmp
  SELECT
	pedagogusId
	,SUM([1]) AS [1], SUM([2]) AS [2], SUM([3]) AS [3], SUM([4]) AS [4], SUM([5]) AS [5], SUM([6]) AS [6], SUM([7]) AS [7], SUM([8]) AS [8], SUM([9]) AS [9], SUM([10]) AS [10],
	SUM([11]) AS [11], SUM([12]) AS [12], SUM([13]) AS [13], SUM([14]) AS [14], SUM([15]) AS [15], SUM([16]) AS [16], SUM([17]) AS [17], SUM([18]) AS [18], SUM([19]) AS [19], SUM([20]) AS [20],
	SUM([21]) AS [21], SUM([22]) AS [22], SUM([23]) AS [23], SUM([24]) AS [24], SUM([25]) AS [25], SUM([26]) AS [26], SUM([27]) AS [27], SUM([28]) AS [28], SUM([29]) AS [29], SUM([30]) AS [30], SUM([31]) AS [31]
  	,SUM(Ossz) as Ossz
  FROM #EgyeniFoglalkozas_tmp
  GROUP BY
	pedagogusId

--============================================
--NEVELÉSSEL-OKTATÁSSAL LE NEM KÖTÖTT MUNKAIDŐ
--============================================
--ÖSSZESEN
	SELECT
   		 pedagogusId
		,Honap
		 ,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
		 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
		 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31],
		 ISNULL([1],0)+ISNULL([2],0)+ISNULL([3],0)+ISNULL([4],0)+ISNULL([5],0)+
		 ISNULL([6],0)+ISNULL([7],0)+ISNULL([8],0)+ISNULL([9],0)+ISNULL([10],0)+
		 ISNULL([11],0)+ISNULL([12],0)+ISNULL([13],0)+ISNULL([14],0)+ISNULL([15],0)+
		 ISNULL([16],0)+ISNULL([17],0)+ISNULL([18],0)+ISNULL([19],0)+ISNULL([20],0)+
		 ISNULL([21],0)+ISNULL([22],0)+ISNULL([23],0)+ISNULL([24],0)+ISNULL([25],0)+
		 ISNULL([26],0)+ISNULL([27],0)+ISNULL([28],0)+ISNULL([29],0)+ISNULL([30],0)+ISNULL([31],0) Ossz
     INTO #LeNemKotottMunkaido_tmp
	FROM
	(
	SELECT
		T_NEMKOTOTTMUNKAIDO_OSSZES.C_TANARID   AS  pedagogusId
		,DATEPART(DAY, C_KEZDETE)	Nap
		,DATEPART(MONTH, C_KEZDETE)	Honap
		,ROUND(CONVERT(FLOAT,(DATEPART(HOUR, C_VEGE - C_KEZDETE) * 60 + DATEPART(MINUTE, C_VEGE - C_KEZDETE))) / 60, 2) Oraszam
	FROM T_NEMKOTOTTMUNKAIDO_OSSZES
	WHERE
			C_MEGTARTOTT = 'T'
		AND T_NEMKOTOTTMUNKAIDO_OSSZES.C_TANARID = @pTanarId
		AND TOROLT = 'F'
		AND DATEPART(MONTH, C_KEZDETE)	= @Honap
		AND DATEPART(MONTH, C_VEGE)		= @Honap
	) Orak
	PIVOT
	(
	SUM(Oraszam)
	FOR Nap IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])
	) Tevekenyseg

  ----ITT ADOM VISSZA A TABLAT=======================================<<<<<<
  SELECT * FROM #LeNemKotottMunkaido_tmp

  SELECT
	pedagogusId
	,SUM([1]) AS [1], SUM([2]) AS [2], SUM([3]) AS [3], SUM([4]) AS [4], SUM([5]) AS [5], SUM([6]) AS [6], SUM([7]) AS [7], SUM([8]) AS [8], SUM([9]) AS [9], SUM([10]) AS [10],
	SUM([11]) AS [11], SUM([12]) AS [12], SUM([13]) AS [13], SUM([14]) AS [14], SUM([15]) AS [15], SUM([16]) AS [16], SUM([17]) AS [17], SUM([18]) AS [18], SUM([19]) AS [19], SUM([20]) AS [20],
	SUM([21]) AS [21], SUM([22]) AS [22], SUM([23]) AS [23], SUM([24]) AS [24], SUM([25]) AS [25], SUM([26]) AS [26], SUM([27]) AS [27], SUM([28]) AS [28], SUM([29]) AS [29], SUM([30]) AS [30], SUM([31]) AS [31]
  	,SUM(Ossz) as Ossz
  FROM #LeNemKotottMunkaido_tmp
  GROUP BY
	pedagogusId


--sorokra (tipusokra) lebontva
	SELECT
	 pedagogusId
	 ,Honap
	 ,TipusId				TipusId
	 ,Tipus					Tipus
	 ,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
 	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31],
	 ISNULL([1],0)+ISNULL([2],0)+ISNULL([3],0)+ISNULL([4],0)+ISNULL([5],0)+
	 ISNULL([6],0)+ISNULL([7],0)+ISNULL([8],0)+ISNULL([9],0)+ISNULL([10],0)+
	 ISNULL([11],0)+ISNULL([12],0)+ISNULL([13],0)+ISNULL([14],0)+ISNULL([15],0)+
	 ISNULL([16],0)+ISNULL([17],0)+ISNULL([18],0)+ISNULL([19],0)+ISNULL([20],0)+
	 ISNULL([21],0)+ISNULL([22],0)+ISNULL([23],0)+ISNULL([24],0)+ISNULL([25],0)+
	 ISNULL([26],0)+ISNULL([27],0)+ISNULL([28],0)+ISNULL([29],0)+ISNULL([30],0)+ISNULL([31],0) Ossz
	INTO #NemKotottSorok_tmp
	FROM
	(
	SELECT
		nemKotottMunkaido.C_TANARID   AS				pedagogusId
		,nemKotottMunkaido.C_TEVEKENYSEGTIPUSA			TipusId
		,tipusNeve.C_NAME								Tipus
		,DATEPART(DAY, nemKotottMunkaido.C_KEZDETE)		Nap
		,DATEPART(MONTH, nemKotottMunkaido.C_KEZDETE)	Honap
		,ROUND(CONVERT(FLOAT,(DATEPART(HOUR, nemKotottMunkaido.C_VEGE - nemKotottMunkaido.C_KEZDETE) * 60 + DATEPART(MINUTE, nemKotottMunkaido.C_VEGE - nemKotottMunkaido.C_KEZDETE))) / 60, 2) Oraszam
	FROM T_NEMKOTOTTMUNKAIDO_OSSZES	nemKotottMunkaido
	INNER JOIN (SELECT DISTINCT ID, C_NAME FROM T_DICTIONARYITEMBASE) tipusNeve ON tipusNeve.ID=nemKotottMunkaido.C_TEVEKENYSEGTIPUSA
	WHERE
			C_MEGTARTOTT='T'
		AND nemKotottMunkaido.C_TANARID = @pTanarId
		AND TOROLT = 'F'
		AND DATEPART(MONTH, C_KEZDETE)	= @Honap
		AND DATEPART(MONTH, C_VEGE)		= @Honap
	) Orak
	PIVOT
	(
	SUM(Oraszam)
	FOR Nap IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])
	) Soronkent

----ITT ADOM VISSZA A TABLAT=======================================<<<<<<
	SELECT * FROM #NemKotottSorok_tmp

	SELECT
	 pedagogusId
	,SUM([1]) AS [1], SUM([2]) AS [2], SUM([3]) AS [3], SUM([4]) AS [4], SUM([5]) AS [5], SUM([6]) AS [6], SUM([7]) AS [7], SUM([8]) AS [8], SUM([9]) AS [9], SUM([10]) AS [10],
	SUM([11]) AS [11], SUM([12]) AS [12], SUM([13]) AS [13], SUM([14]) AS [14], SUM([15]) AS [15], SUM([16]) AS [16], SUM([17]) AS [17], SUM([18]) AS [18], SUM([19]) AS [19], SUM([20]) AS [20],
	SUM([21]) AS [21], SUM([22]) AS [22], SUM([23]) AS [23], SUM([24]) AS [24], SUM([25]) AS [25], SUM([26]) AS [26], SUM([27]) AS [27], SUM([28]) AS [28], SUM([29]) AS [29], SUM([30]) AS [30], SUM([31]) AS [31]
  	,SUM(Ossz) as Ossz
	FROM #NemKotottSorok_tmp
	GROUP BY
		pedagogusId

-- Túlóra
	SELECT
		pedagogusId
		,OsztalyCsoportID
		,OsztalyCsoportNev
		,Honap
		,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
		 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
		 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31],
		 ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) +
		 ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) + ISNULL([9], 0) + ISNULL([10], 0) +
		 ISNULL([11], 0) + ISNULL([12], 0) + ISNULL([13], 0) + ISNULL([14], 0) + ISNULL([15], 0) +
		 ISNULL([16], 0) + ISNULL([17], 0) + ISNULL([18], 0) + ISNULL([19], 0) + ISNULL([20], 0) +
		 ISNULL([21], 0) + ISNULL([22], 0) + ISNULL([23], 0) + ISNULL([24], 0) + ISNULL([25], 0) +
		 ISNULL([26], 0) + ISNULL([27], 0) + ISNULL([28], 0) + ISNULL([29], 0) + ISNULL([30], 0) + ISNULL([31], 0) Ossz
     INTO #Tulora_tmp
	FROM
	(
	SELECT
		tao.C_ORATULAJDONOSID   AS  pedagogusId
		,DATEPART(DAY, tao.C_DATUM)	Nap
		,DATEPART(MONTH,tao.C_DATUM) Honap
		,ocs.C_NEV					OsztalyCsoportNev
		,tao.C_OSZTALYCSOPORTID		oraid
		,tao.C_OSZTALYCSOPORTID		OsztalyCsoportID
		,IIF(tao.C_CSENGETESIRENDORAID IS NOT NULL,
				1,
				ROUND(CONVERT(FLOAT,(DATEPART(HOUR, C_ORAVEGE - C_ORAKEZDETE) * 60 + DATEPART(MINUTE, C_ORAVEGE - C_ORAKEZDETE))) / IIF(cs.Id IS NULL OR cs.C_TIPUSA IN (SELECT ID FROM fnGetTanoraiCeluCsoportTipusok(@tanevId)), 45, 60), 2)
		)SumOra
	FROM T_TANITASIORA_OSSZES tao
		INNER JOIN T_OSZTALYCSOPORT_OSSZES ocs on ocs.ID=tao.C_OSZTALYCSOPORTID AND (ocs.C_FELADATKATEGORIAID = @feladatKategoriaId OR @feladatKategoriaId IS NULL)
		LEFT JOIN T_CSOPORT_OSSZES cs ON cs.ID = ocs.ID
		LEFT JOIN T_FOGLALKOZAS_OSSZES fog ON fog.ID = C_FOGLALKOZASID
	WHERE
			DATEPART(MONTH, tao.C_DATUM) = @Honap
		AND tao.C_ORATULAJDONOSID = @pTanarId
		AND tao.TOROLT = 'F'
		AND tao.C_TANEVID = @tanevId
		AND tao.C_TULORA = 'T'
		AND tao.C_MEGTARTOTT = 'T'
	)a
	PIVOT
	(
	SUM(SumOra)
	FOR Nap IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])

	) Tulora

  ----ITT ADOM VISSZA A TABLAT=======================================<<<<<<
  SELECT * FROM #Tulora_tmp
  SELECT
  	pedagogusId
	,SUM([1]) AS [1], SUM([2]) AS [2], SUM([3]) AS [3], SUM([4]) AS [4], SUM([5]) AS [5], SUM([6]) AS [6], SUM([7]) AS [7], SUM([8]) AS [8], SUM([9]) AS [9], SUM([10]) AS [10],
	SUM([11]) AS [11], SUM([12]) AS [12], SUM([13]) AS [13], SUM([14]) AS [14], SUM([15]) AS [15], SUM([16]) AS [16], SUM([17]) AS [17], SUM([18]) AS [18], SUM([19]) AS [19], SUM([20]) AS [20],
	SUM([21]) AS [21], SUM([22]) AS [22], SUM([23]) AS [23], SUM([24]) AS [24], SUM([25]) AS [25], SUM([26]) AS [26], SUM([27]) AS [27], SUM([28]) AS [28], SUM([29]) AS [29], SUM([30]) AS [30], SUM([31]) AS [31]
  	,SUM(Ossz) as Ossz
  FROM #Tulora_tmp
  GROUP BY
	pedagogusId

  --==================
  --TABLA OSSZESITESEK
  --==================
  SELECT
	pedagogusId,
	SUM([1]) O1, SUM([2]) O2, SUM([3]) O3, SUM([4]) O4, SUM([5]) O5, SUM([6]) O6, SUM([7]) O7, SUM([8]) O8, SUM([9]) O9, SUM([10]) O10,
	SUM([11]) O11, SUM([12]) O12, SUM([13]) O13, SUM([14]) O14, SUM([15]) O15, SUM([16]) O16, SUM([17]) O17, SUM([18]) O18, SUM([19]) O19, SUM([20]) O20,
 	SUM([21]) O21, SUM([22]) O22, SUM([23]) O23, SUM([24]) O24, SUM([25]) O25, SUM([26]) O26, SUM([27]) O27, SUM([28]) O28, SUM([29]) O29, SUM([30]) O30, SUM([31]) O31,
	SUM(Ossz) AS Ossz
  FROM
  (
  SELECT * FROM #MegtartottOrakHelyesitesek_tmp
  UNION ALL
  SELECT * FROM #NemSzakszeruHelyetesites_tmp
  UNION ALL
  SELECT * FROM #OraOsszevonas_tmp
  UNION ALL
  SELECT * FROM #SzakszeruHelyetesites_tmp
  UNION ALL
  SELECT * FROM #TanoranKivuli_tmp
  UNION ALL
  SELECT
		pedagogusId
		,NULL
		,NULL
		,Honap
		,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
		 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
		 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31],
		 Ossz
	 FROM #LeNemKotottMunkaido_tmp
  UNION ALL
  SELECT * FROM #EgyeniFoglalkozas_tmp

  ) as TablaOsszesito
   GROUP BY
  pedagogusId

  CREATE TABLE #helyettesitesek (pedagogusId int, Nap int, Honap int, SumOra float, HelyettesitesTipusa int)
  INSERT INTO #helyettesitesek
  SELECT
		tao.C_HELYETTESITOTANARID   AS  pedagogusId
		,DATEPART(DAY, tao.C_DATUM)	AS	Nap
		,DATEPART(MONTH,tao.C_DATUM) AS	Honap
		,IIF(tao.C_CSENGETESIRENDORAID IS NOT NULL,
				1,
				ROUND(CONVERT(FLOAT,(DATEPART(HOUR, C_ORAVEGE - C_ORAKEZDETE) * 60 + DATEPART(MINUTE, C_ORAVEGE - C_ORAKEZDETE))) / IIF(cs.Id IS NULL OR cs.C_TIPUSA IN (SELECT ID FROM fnGetTanoraiCeluCsoportTipusok(@tanevId)), 45, 60), 2)
			) AS SumOra
    ,tao.C_HELYETTESITESTIPUSA AS HelyettesitesTipusa
	FROM T_TANITASIORA_OSSZES AS tao
		INNER JOIN T_OSZTALYCSOPORT_OSSZES AS ocs ON ocs.id=tao.C_OSZTALYCSOPORTID AND (ocs.C_FELADATKATEGORIAID = @feladatKategoriaId OR @feladatKategoriaId IS NULL)
		LEFT JOIN T_CSOPORT_OSSZES AS cs ON cs.ID = ocs.ID
	WHERE
			tao.C_DATUM < @backDate
		AND tao.C_HELYETTESITOTANARID = @pTanarId
		AND tao.TOROLT = 'F'
		AND tao.C_MEGTARTOTT = 'T'
		AND tao.C_TANEVID = @tanevId
		AND (tao.C_PARHUZAMOSORA = 'F' OR tao.C_PARHUZAMOSORA IS NULL)

  SELECT
		pedagogusId,
    HelyettesitesTipusa
		,[9],[10],[11],[12],[1],[2],[3],[4],[5],[6],[7],[8]
    ,ISNULL([9], 0) + ISNULL([10], 0) + ISNULL([11], 0) + ISNULL([12], 0) + ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) + ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) AS Ossz
  FROM (
	  SELECT
		  pedagogusId
		  ,Honap
		  ,SumOra
      ,d.C_NAME AS HelyettesitesTipusa
	  FROM #helyettesitesek AS h
      INNER JOIN T_DICTIONARYITEMBASE AS d ON h.HelyettesitesTipusa = d.ID AND d.C_TANEVID = @tanevId AND d.C_INTEZMENYID = @pIntezmenyId AND d.TOROLT = 'F'
    )a
  PIVOT (
    SUM(SumOra) FOR Honap IN ([9],[10],[11],[12],[1],[2],[3],[4],[5],[6],[7],[8])
  ) piv

END
GO