AS
BEGIN
SET NOCOUNT ON

		
IF OBJECT_ID('dbo.cacheEZViewer_Usage_FileInPrg') IS NOT NULL  DROP TABLE dbo.cacheEZViewer_Usage_FileInPrg	
SELECT   FileName, FileID, LogicalName,
         ProgramID, ProgramName, ProgramTypeID,
         pth2.PathStr AS ProgramPath, ProgStartRow, ProgStartCol, ProgEndRow, ProgEndCol,
	    Ancestor, AncestorID, AncestorTypeId,
	    Description, StatementStartRow, StatementStartColumn, StatementEndRow, StatementEndColumn, pth1.PathStr AS StmtPathStr
INTO dbo.cacheEZViewer_Usage_FileInPrg
FROM(
		  SELECT       CASE  WHEN prog.ProgramTypeID = 19
                         THEN CASE
                              WHEN FilesForSCL.Name IS NULL THEN core.FileName
                              ELSE FilesForSCL.Name
                              END
					  ELSE CASE
							 WHEN SCLSystemFiles1.Name IS NULL
							 THEN core.FileName
							 ELSE SCLSystemFiles1.Name
						  END
					  END AS FileName,
			    core.FileID,
			    core.LogicalName,
			    prog.ProgramID,
			    prog.ProgramName,
			    prog.ProgramTypeID,
			    prog.ProgramPathID,
			    prog.ProgStartRow,
			    prog.ProgStartCol,
			    prog.ProgEndRow,
			    prog.ProgEndCol,
			    prog.Ancestor,
			    prog.AncestorID,
			    prog.AncestorTypeID,
			    core.Description,
			    core.StatementStartRow,
			    core.StatementStartColumn,
			    core.StatementEndRow,
			    core.StatementEndColumn,
			    core.StmtPathID
		  FROM (
				SELECT  -- core data
					   os.ProgID,
					   os.PathID AS StmtPathID,
			             os.StartRow AS StatementStartRow,
			             os.StartCol AS StatementStartColumn,
			             os.EndRow AS StatementEndRow,
			             os.EndCol AS StatementEndColumn,
					   st.Description,
					   f.FileID,
					   f.Name AS FileName,
					   f.OccurID AS FileOccurID,
					   f.LogicalName
				FROM dbo.StatementReference sr
					 INNER JOIN dbo.OccurrencesStmt os ON sr.OccurID = os.OccurID
					 INNER JOIN dbo.Statements st ON sr.StatementType = st.StatementType
					 INNER JOIN dbo.Files f ON sr.ResourceID = f.FileID
				WHERE sr.ResourceType = 9
					  AND  os.PathID IS NOT NULL
			   )core
			    INNER JOIN ( -- programs
						      SELECT p.ProgramID, p.ProgramName, p.ProgramTypeID,
								 occ.StartRow AS ProgStartRow, occ.StartCol AS ProgStartCol, occ.EndRow AS ProgEndRow, occ.EndCol AS ProgEndCol,
								 occ.PathID AS ProgramPathID,
								 p.Ancestor, pa.ProgramID AS AncestorID, pa.ProgramTypeID AS AncestorTypeID
							  FROM dbo.Occurrences occ
								   INNER JOIN dbo.Programs p ON p.OccurID = occ.OccurID
                                           LEFT OUTER JOIN (select DISTINCT tp.ProgramID, ProgramTypeID, tpa.AliasName as ProgramName from dbo.Programs tp
                                                                INNER JOIN ProgramAliases tpa ON tpa.ProgramID = tp.ProgramID) pa
                                                                  ON pa.ProgramName = p.Ancestor AND pa.ProgramTypeID = 13
							  WHERE occ.PathID IS NOT NULL
						) prog ON core.ProgID = prog.ProgramID
			    LEFT OUTER JOIN dbo.SCLSystemFiles AS FilesForSCL ON core.FileOccurID = FilesForSCL.OccurID
                                                                       AND ((core.FileID = FilesForSCL.fileId AND prog.ProgramTypeID IN(13, 19))
                                                                              OR
															  (core.FileName = FilesForSCL.SystemFileId AND prog.ProgramTypeID = 1))
			    LEFT OUTER JOIN dbo.SCLSystemFiles ON core.FileID = SCLSystemFiles.fileId AND prog.ProgramTypeID IN (13, 19)
			    LEFT OUTER JOIN dbo.SCLSystemFiles AS SCLSystemFiles1 ON core.FileName = SCLSystemFiles1.SystemFileId AND prog.ProgramTypeID = 1
			  GROUP BY  core.FileID, core.LogicalName,
			            prog.ProgramID, prog.ProgramName, prog.ProgramTypeID, prog.ProgramPathID, prog.ProgStartRow, prog.ProgStartCol, prog.ProgEndRow, prog.ProgEndCol,
			            prog.Ancestor, prog.AncestorID, prog.AncestorTypeID, core.Description, core.StatementStartRow, core.StatementStartColumn, core.StatementEndRow, core.StatementEndColumn, core.StmtPathID,
				       CASE  WHEN prog.ProgramTypeID = 19
						  THEN CASE
							 WHEN FilesForSCL.Name IS NULL THEN core.FileName
							 ELSE FilesForSCL.Name
							 END
						  ELSE CASE
								WHEN SCLSystemFiles1.Name IS NULL
								THEN core.FileName
								ELSE SCLSystemFiles1.Name
							 END
						  END
		
		  )c
		   LEFT OUTER JOIN  dbo.Paths pth1 ON pth1.PathID = c.StmtPathID
		   LEFT OUTER JOIN  dbo.Paths pth2 ON pth2.PathID = c.ProgramPathID		

	
END