AS set NOCOUNT ON;

IF OBJECT_ID('tempdb..#programWithAncestor') IS NOT NULL DROP TABLE #programWithAncestor
			 SELECT * INTO #programWithAncestor 
			 FROM   fGetProgram_Ancestor_Aliases (NULL)
			 
SELECT pgm.ProgramName,
	 COUNT(DISTINCT OccurrencesStmt.OccurID) AS NumOfStatements, pgm.AncestorName, pgm.ProgramID, pgm.AncestorID
  FROM  OccurrencesStmt INNER JOIN #programWithAncestor pgm ON OccurrencesStmt.ProgID=pgm.ProgramID
WHERE (pgm.OccurID<>0)
  And ((pgm.ProgramID) In (Select * from #numeric_param_temp))
GROUP BY pgm.AncestorName, pgm.ProgramName, pgm.ProgramID, pgm.AncestorID
order by pgm.AncestorName, pgm.ProgramName