AS
BEGIN
SET NOCOUNT ON


IF OBJECT_ID('dbo.cacheEZViewer_Usage_Includes') IS NOT NULL  DROP TABLE dbo.cacheEZViewer_Usage_Includes
SELECT
	   StatementTypeId, StatementTypeName,
	   pth1.PathStr AS StmtPathStr,
	   ResourceType, StatementStartRow, StatementStartColumn, StatementEndRow, StatementEndColumn, IncludeID, IncludeName, IncludePathID,
	   CASE WHEN IncludeName = pth2.PathStr THEN NULL
	   ELSE pth2.PathStr
	   END
	   AS IncludePath,
	   IncludeStartRow, IncludeStartCol, IncludeEndRow, IncludeEndCol, ProgramID, ProgramName, ProgramTypeID,
	   pth3.PathStr AS ProgramPath, ProgStartRow, ProgStartCol, ProgEndRow, ProgEndCol, Ancestor, AncestorID, AncestorTypeID, AncestorStartRow, AncestorStartCol, AncestorEndRow, AncestorEndCol,
	   StmtOccurId
 INTO dbo.cacheEZViewer_Usage_Includes
 FROM (
		  SELECT
			    core.StatementTypeId,
			    core.StatementTypeName,
			    core.PathID AS StmtPathID,
			    core.ResourceType,
			    core.StartRow AS StatementStartRow,
			    core.StartCol AS StatementStartColumn,
			    core.EndRow AS StatementEndRow,
			    core.EndCol AS StatementEndColumn,
			    res.ResourceID AS IncludeID,
			    res.Name AS IncludeName,
			    res.PathID AS IncludePathID,
			    res.StartRow AS IncludeStartRow,
			    res.StartCol AS IncludeStartCol,
			    res.EndRow AS IncludeEndRow,
			    res.EndCol AS IncludeEndCol,
			    prog.ProgramID,
			    prog.ProgramName,
			    prog.ProgramTypeID,
			    prog.PathID AS ProgramPathID,
			    prog.StartRow AS ProgStartRow,
			    prog.StartCol AS ProgStartCol,
			    prog.EndRow AS ProgEndRow,
			    prog.EndCol AS ProgEndCol,
			    prog.Ancestor,
			    aprog.ProgramID AS AncestorID,
			    aprog.ProgramTypeID AS AncestorTypeID,
			    aprog.StartRow AS AncestorStartRow,
			    aprog.StartCol AS AncestorStartCol,
			    aprog.EndRow AS AncestorEndRow,
			    aprog.EndCol AS AncestorEndCol,
			    core.StmtOccurId
		  FROM (  -- distinct core data
				SELECT
					   os.ProgID,
					   os.PathID,
					   os.StartRow,
					   os.StartCol,
					   os.EndRow,
					   os.EndCol,
					   sr.ResourceID,
					   sr.ResourceType,
					   st.StatementType AS StatementTypeId,
					   st.Description AS StatementTypeName,
					   sr.OccurID AS StmtOccurId
				FROM dbo.StatementReference sr
					 INNER JOIN dbo.OccurrencesStmt os ON sr.OccurID = os.OccurID
					 LEFT OUTER JOIN dbo.Statements st ON sr.StatementType = st.StatementType
				WHERE sr.ResourceType IN ( 13, 57, 60, 67, 99)
					  AND  os.PathID IS NOT NULL
			   )core
			    INNER JOIN ( -- programs
						    SELECT p.ProgramID, p.ProgramName, p.ProgramTypeID, p.Ancestor,
								 occ.StartRow, occ.StartCol, occ.EndRow, occ.EndCol
								,occ.PathID
							  FROM dbo.Occurrences occ
								   INNER JOIN (SELECT p1.ProgramID, AliasName as ProgramName, ProgramTypeID, Ancestor, OccurId
                                                                                FROM dbo.Programs p1 INNER JOIN dbo.ProgramAliases p2 ON p2.ProgramId = p1.ProgramId and p2.AliasType = 0) p ON p.OccurID = occ.OccurID
							  WHERE occ.PathID IS NOT NULL
						) prog ON core.ProgID = prog.ProgramID
			   INNER JOIN ( -- resources
						    SELECT r.ResourceID, r.Name, occ.StartRow, occ.StartCol, occ.EndRow, occ.EndCol,
								 occ.PathID
							FROM dbo.Occurrences occ
								   INNER JOIN dbo.Resources r ON r.OccurID = occ.OccurID
							WHERE occ.PathID IS NOT NULL
						) res ON res.ResourceID = core.ResourceID
			    LEFT OUTER JOIN ( -- ancestor
							   SELECT  p.ProgramID, p.ProgramName, p.ProgramTypeID,
										    occ.StartRow, occ.StartCol, occ.EndRow, occ.EndCol
								   FROM  (SELECT DISTINCT p1.ProgramId, ProgramTypeID, AliasName as ProgramName, OccurID FROM dbo.Programs p1 INNER JOIN dbo.ProgramAliases  p2 ON p1.ProgramID = p2.ProgramID) p
										LEFT OUTER JOIN dbo.Occurrences occ ON p.OccurID = occ.OccurID
								   WHERE p.ProgramTypeID = 8
							) aprog  ON aprog.ProgramName = prog.Ancestor
			  GROUP BY  core.StatementTypeId, core.StatementTypeName,  core.PathID, core.ResourceType,
					  core.StartRow , core.StartCol, core.EndRow, core.EndCol, core.StmtOccurId, res.ResourceID, res.Name, res.PathID, res.StartRow, res.StartCol, res.EndRow, res.EndCol,
					  prog.ProgramID, prog.ProgramName, prog.ProgramTypeID, prog.PathID,  prog.StartRow, prog.StartCol, prog.EndRow, prog.EndCol, prog.Ancestor,
					  aprog.ProgramID, aprog.ProgramTypeID, aprog.StartRow, aprog.StartCol, aprog.EndRow, aprog.EndCol
		  )c
		   LEFT OUTER JOIN  dbo.Paths pth1 ON pth1.PathID = c.StmtPathID
		   LEFT OUTER JOIN  dbo.Paths pth2 ON pth2.PathID = c.IncludePathID
		   LEFT OUTER JOIN  dbo.Paths pth3 ON pth3.PathID = c.ProgramPathID

END