LANGUAGE SQL
BEGIN


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

					

				INSERT INTO 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
				 FROM (
						  SELECT DISTINCT
							    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 StatementReference sr
									 INNER JOIN OccurrencesStmt os ON sr.OccurID = os.OccurID
									 LEFT OUTER JOIN 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 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
															  ) 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 Occurrences occ
												   INNER JOIN 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
																        pg.ProgramID,
																		pg.ProgramTypeID,
																		pg.OccurID,
																		pa.AliasName AS ProgramName
																  FROM Programs pg
																	   INNER JOIN ProgramAliases pa ON pg.ProgramID = pa.ProgramId
																  WHERE pg.ProgramTypeID = 8
															  ) p
														LEFT OUTER JOIN Occurrences occ ON p.OccurID = occ.OccurID
											) aprog  ON aprog.ProgramName = prog.Ancestor
							
						  )c
						   LEFT OUTER JOIN  Paths pth1 ON pth1.PathID = c.StmtPathID
						   LEFT OUTER JOIN  Paths pth2 ON pth2.PathID = c.IncludePathID
						   LEFT OUTER JOIN  Paths pth3 ON pth3.PathID = c.ProgramPathID ;


END 
