AS
BEGIN
SET NOCOUNT ON



IF OBJECT_ID('dbo.cacheEZViewer_Usage_DBI') IS NOT NULL  DROP TABLE dbo.cacheEZViewer_Usage_DBI 
SELECT  ProgramID, ProgramName, ProgramTypeID, Ancestor,
        pth2.PathStr AS ProgramPath, ProgStartRow, ProgStartCol, ProgEndRow, ProgEndCol,
	   Description, 
	   pth1.PathStr AS StmtPathStr, 
	   StatementStartRow, StatementStartColumn, StatementEndRow, StatementEndColumn, 
	   ResourceID, ResourceName, OccurID, DBIPattern
INTO dbo.cacheEZViewer_Usage_DBI
FROM (
		  SELECT -- distinct core data
			    prog.ProgramID,
			    prog.ProgramName,
			    prog.ProgramTypeID,
			    prog.Ancestor,
			    prog.PathID AS ProgramPathID,
			    prog.StartRow AS ProgStartRow,
			    prog.StartCol AS ProgStartCol,
			    prog.EndRow AS ProgEndRow,
			    prog.EndCol AS ProgEndCol,
			    core.Description,
			    core.PathID AS StmtPathID,
			    core.StartRow AS StatementStartRow,
			    core.StartCol AS StatementStartColumn,
			    core.EndRow AS StatementEndRow,
			    core.EndCol AS StatementEndColumn,
			    core.ResourceID,
			    core.ResourceName,
			    core.ResourcesOccurID AS OccurID, 
			    core.DBIPattern   
		  FROM ( 
				SELECT  -- core data
					   os.ProgID,
					   os.PathID,
					   os.StartRow,
					   os.StartCol,
					   os.EndRow,
					   os.EndCol,
					   r.ResourceID,
					   r.Name AS ResourceName,
				        r.OccurID AS ResourcesOccurID, 
					   st.Description, 
					   dbiacc.DBIPattern
				FROM dbo.StatementReference sr
					 INNER JOIN dbo.OccurrencesStmt os ON sr.OccurID = os.OccurID
					 INNER JOIN dbo.Resources r ON sr.ResourceID = r.ResourceID 
					 INNER JOIN dbo.Statements st ON sr.StatementType = st.StatementType
					 LEFT OUTER JOIN (
					                   SELECT r1.Name AS DBIPattern, sr1.OccurID
					                   FROM dbo.StatementReference sr1
								          INNER JOIN dbo.Resources r1 ON sr1.ResourceID = r1.ResourceID
								    WHERE sr1.ResourceType = 191 
					                 ) dbiacc ON os.OccurID = dbiacc.OccurID
				WHERE sr.ResourceType = 190 
					  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 OccurID, ProgramTypeID, tp.ProgramID, tpa.AliasName as ProgramName, Ancestor from dbo.Programs tp
                                                                                INNER JOIN ProgramAliases tpa on tpa.ProgramId = tp.ProgramId and tpa.AliasType = 0 ) p 
                                                ON p.OccurID = occ.OccurID AND p.ProgramTypeID = 1
							  WHERE occ.PathID IS NOT NULL
						) prog ON core.ProgID = prog.ProgramID  
			  GROUP BY  prog.ProgramID, prog.ProgramName, prog.ProgramTypeID, prog.Ancestor,
			            prog.PathID,  prog.StartRow, prog.StartCol, prog.EndRow, prog.EndCol,
					  core.Description,  core.PathID, core.StartRow , core.StartCol, core.EndRow, core.EndCol, 
					  core.ResourceID, core.ResourceName, core.ResourcesOccurID, core.DBIPattern
		  
		  )c
		   LEFT OUTER JOIN  dbo.Paths pth1 ON pth1.PathID = c.StmtPathID
		   LEFT OUTER JOIN  dbo.Paths pth2 ON pth2.PathID = c.ProgramPathID			

 
END
