LANGUAGE SQL
BEGIN


		 BEGIN
		      COMMIT;
			  TRUNCATE TABLE cacheEZViewer_Usage_DAL IMMEDIATE ;
              --DELETE FROM cacheEZViewer_Usage_DAL ;
         END;


		INSERT INTO cacheEZViewer_Usage_DAL
		         (ProgramID, ProgramName, ProgramTypeID, Ancestor, ProgramPath, ProgStartRow, ProgStartCol, ProgEndRow, ProgEndCol,
		          Description, StmtPathStr, StatementStartRow, StatementStartColumn, StatementEndRow, StatementEndColumn,
		          ResourceID, ResourceName, OccurID
		         )
		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
				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
						  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
								FROM StatementReference sr
									 INNER JOIN OccurrencesStmt os ON sr.OccurID = os.OccurID
									 INNER JOIN Resources r ON sr.ResourceID = r.ResourceID
									 INNER JOIN Statements st ON sr.StatementType = st.StatementType
								WHERE sr.ResourceType IN (189) -- DAL File
									  AND  os.PathID > 0
							   )core
							    INNER JOIN ( -- programs
										    SELECT p.ProgramID, p.ProgramName, p.ProgramTypeID, p.Ancestor,
												 occ.StartRow, occ.StartCol, occ.EndRow, occ.EndCol
												,occ.PathID
											  FROM Occurrences occ
												     INNER JOIN
																(
																	 SELECT  pg.ProgramID,
																			 pg.Ancestor,
																			 pg.ProgramTypeID,
																			 pg.OccurID,
																			 pa.AliasName AS ProgramName
																	  FROM Programs pg
																		   INNER JOIN ProgramAliases pa ON pg.ProgramID = pa.ProgramId AND pa.AliasType = 0
																	  WHERE pg.ProgramTypeID = 1	
																  ) p ON p.OccurID = occ.OccurID
											  WHERE occ.PathID > 0
										) 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 					
						  )c
						   LEFT OUTER JOIN  Paths pth1 ON pth1.PathID = c.StmtPathID
						   LEFT OUTER JOIN  Paths pth2 ON pth2.PathID = c.ProgramPathID;
		


END 
