DROP PROCEDURE IF EXISTS uspGetCsengetesiiRendKapcsolodoOra 
GO
  CREATE PROCEDURE uspGetCsengetesiiRendKapcsolodoOra
      @pCsengetesiRendId INT
     ,@pTanevId INT
     ,@pIntezmenyId INT		
  AS
  BEGIN
    SET NOCOUNT ON;

    SELECT csr.ID
      ,IIF((
          CASE 
            WHEN EXISTS (
                SELECT TOP 1 1
                FROM T_ORARENDIORA_OSSZES oo
                WHERE C_CSENGETESIRENDID = csr.ID
                  AND oo.TOROLT = 'F'
                )
              THEN 1
            WHEN EXISTS (
                SELECT TOP 1 1
                FROM T_TANITASIORA_OSSZES tao 
                WHERE C_CSENGETESIRENDID = csr.ID
                  AND tao.TOROLT = 'F'
                )
              THEN 2
            ELSE NULL
            END
          ) IS NULL, 'false', 'true') AS IsVanOraKapcsolodas
      ,IIF(csr.C_AKTIV = 'T', 'true', 'false') AS IsAktiv
      ,MIN(csro.C_ORASZAM) AS MinOraszam
      ,MAX(csro.C_ORASZAM) AS MaxOraszam
    FROM T_CSENGETESIREND_OSSZES csr
    LEFT JOIN T_CSENGETESIRENDORA_OSSZES csro ON csro.C_CSENGETESIRENDID = csr.ID
      AND csro.TOROLT = 'F'
    WHERE csr.ID = @pCsengetesiRendId
      AND csr.TOROLT = 'F'
      AND csr.C_TANEVID = @pTanevId
      AND csr.C_INTEZMENYID = @pIntezmenyId
    GROUP BY csr.ID
      ,csr.C_AKTIV
  END
GO


