SQL : this is query is taking too much of time : 
SQL : this is query is taking too much of time for 12 records, event indexes also created.
    SELECT p.AnchorDate
    		,'Active' StatusDefinition
    		,count(1) PatientCount
    		,6 AS SNO
    	FROM (
    		SELECT DISTINCT pp.PatientID
    			,ad.AnchorDate
    		FROM PatientProgram pp WITH (NOLOCK)
    		INNER JOIN #tblMonth ad ON ad.AnchorDate = CASE 
    				WHEN ad.AnchorDate BETWEEN DATEADD(dd, - (DAY(pp.EnrollmentStartDate) - 1), pp.EnrollmentStartDate)
    						AND  EOMONTH (ISNULL(pp.EnrollmentEndDate, '9999-12-31'))
    							
    					THEN ad.AnchorDate
    				ELSE NULL
    				END
    		WHERE NOT EXISTS (
    				SELECT 1
    				FROM #ManagedPopulation m
    				WHERE m.tKeyId = pp.ProgramID
    				)
    			AND pp.ProgramID != 4331
    		) p
    	GROUP BY p.AnchorDate;