AS
BEGIN
   SET NOCOUNT ON

-- EXEC [dbo].[EZViewer_Usage_IMSDB_Logical]
--SELECT * FROM dbo.cacheEZViewer_Usage_IMSDB_Logical

IF OBJECT_ID('tempdb..#programWithAncestor') IS NOT NULL DROP TABLE #programWithAncestor
			 SELECT * INTO #programWithAncestor 
			 FROM   fGetProgram_Ancestor_Aliases (NULL)

IF OBJECT_ID('dbo.cacheEZViewer_Usage_IMSDB_Logical') IS NOT NULL  DROP TABLE dbo.cacheEZViewer_Usage_IMSDB_Logical
SELECT   LogicalSegmentId, LogicalSegmentName,
         LogicalSegmentStartRow, LogicalSegmentStartCol, LogicalSegmentEndRow, LogicalSegmentEndCol,

         LogicalSegmentDBId, LogicalSegmentDBName, LogicalSegmentDBAccessTypeId AS LogicalAccessTypeId,
	    LogicalSegmentDBStartRow AS LogicalIMSDBStartRow, LogicalSegmentDBStartCol AS LogicalIMSDBStartCol, LogicalSegmentDBEndRow AS LogicalIMSDBEndRow, LogicalSegmentDBEndCol AS LogicalIMSDBEndCol, pth1.PathStr AS LogicalIMSDBPath,
	

	    PhysicalSegmentId AS SegmentId, PhysicalSegmentName AS SegmentName,
	    PhysicalSegmentStartRow AS SegmentStartRow, PhysicalSegmentStartCol AS SegmentStartCol, PhysicalSegmentEndRow AS SegmentEndRow,  PhysicalSegmentEndCol AS SegmentEndCol,


         PhysicalSegmentDBId AS DBId, PhysicalSegmentDBName AS DBName, PhysicalSegmentDBAccessTypeId AS AccessTypeId,
	    PhysicalSegmentDBStartRow AS IMSDBStartRow, PhysicalSegmentDBStartCol AS IMSDBStartCol, PhysicalSegmentDBEndRow AS IMSDBEndRow,  PhysicalSegmentDBEndCol AS IMSDBEndCol,  pth2.PathStr AS IMSDBPath,


        StatementTypeId, Description, StatementStartRow, StatementStartColumn, StatementEndRow, StatementEndColumn, pth3.PathStr AS StmtPathStr,
        ProgramName, ProgramID, ProgramTypeID, pth4.PathStr AS ProgramPath, ProgStartRow, ProgStartCol, ProgEndRow, ProgEndCol, Ancestor, AncestorID, AncestorTypeID,
        FieldID, FieldName, pth5.PathStr AS FieldPath, FieldStartRow, FieldStartCol, FieldEndRow, FieldEndCol, FieldSeq
INTO dbo.cacheEZViewer_Usage_IMSDB_Logical
FROM (
		  SELECT
		           sgmt.LogicalSegmentId,
				 sgmt.LogicalSegmentName,
				 occ_L_SG.StartRow AS LogicalSegmentStartRow,
				 occ_L_SG.StartCol AS LogicalSegmentStartCol,
				 occ_L_SG.EndRow   AS LogicalSegmentEndRow,
				 occ_L_SG.EndCol   AS LogicalSegmentEndCol,
				 occ_L_SG.PathID   AS LogicalSegmentPathID,

				 sgmt.LogicalSegmentDBId,
				 sgmt.LogicalSegmentDBName,
				 sgmt.LogicalSegmentDBAccessTypeId,
				 occ_L_DB.StartRow AS LogicalSegmentDBStartRow,
				 occ_L_DB.StartCol AS LogicalSegmentDBStartCol,
				 occ_L_DB.EndRow   AS LogicalSegmentDBEndRow,
				 occ_L_DB.EndCol   AS LogicalSegmentDBEndCol,
				 occ_L_DB.PathID   AS LogicalSegmentDBPathID,

				 sgmt.PhysicalSegmentId,
				 sgmt.PhysicalSegmentName,
				 occ_P_SG.StartRow AS PhysicalSegmentStartRow,
				 occ_P_SG.StartCol AS PhysicalSegmentStartCol,
				 occ_P_SG.EndRow   AS PhysicalSegmentEndRow,
				 occ_P_SG.EndCol   AS PhysicalSegmentEndCol,
				 occ_P_SG.PathID   AS PhysicalSegmentPathID,

				 sgmt.PhysicalSegmentDBId,
				 sgmt.PhysicalSegmentDBName,
				 sgmt.PhysicalSegmentDBAccessTypeId,
				 occ_P_DB.StartRow AS PhysicalSegmentDBStartRow,
				 occ_P_DB.StartCol AS PhysicalSegmentDBStartCol,
				 occ_P_DB.EndRow   AS PhysicalSegmentDBEndRow,
				 occ_P_DB.EndCol   AS PhysicalSegmentDBEndCol,
				 occ_P_DB.PathID   AS PhysicalSegmentDBPathID,

				core.Description,
				core.StatementTypeID,
				core.StartRow AS StatementStartRow,
				core.StartCol AS StatementStartColumn,
				core.EndRow AS StatementEndRow,
				core.EndCol AS StatementEndColumn,
				core.PathID AS StmtPathID,

				prog.ProgramName,
				prog.ProgramID,
				prog.ProgramTypeID,
				prog.PathID AS ProgramPathID,
				prog.StartRow AS ProgStartRow,
				prog.StartCol AS ProgStartCol,
				prog.EndRow AS ProgEndRow,
				prog.EndCol AS ProgEndCol,
				prog.Ancestor,
				prog.AncestorID,
				prog.AncestorTypeID,
				field.FieldId,
				COALESCE(field.FieldName, 'N/A') AS FieldName,
				field.PathID AS FieldPathId,
				field.StartRow AS FieldStartRow,
				field.StartCol AS FieldStartCol,
				field.EndRow AS FieldEndRow,
				field.EndCol AS FieldEndCol,
				field.Seq AS FieldSeq
		  FROM (
				SELECT  sr.ResourceID,
					   sr.OccurId,
					   os.ProgID,
					   os.PathID,
					   os.StartRow,
					   os.StartCol,
					   os.EndRow,
					   os.EndCol,
					   st.StatementType AS StatementTypeID,
					   st.Description
				FROM dbo.StatementReference sr
					 INNER JOIN dbo.OccurrencesStmt os ON sr.OccurID = os.OccurID
					 INNER JOIN dbo.Statements st ON sr.StatementType = st.StatementType
				WHERE sr.ResourceType = 209 -- IMS SEGMENT
					  AND  os.PathID IS NOT NULL

			    )core
			    INNER JOIN ( -- programs
						      SELECT p.ProgramID, p.ProgramName, p.ProgramTypeID,
								   occ.StartRow, occ.StartCol, occ.EndRow, occ.EndCol, occ.PathID, p.AncestorName AS Ancestor, p.AncestorID AS AncestorID, pa.ProgramTypeID AS AncestorTypeID
							  FROM #programWithAncestor p
								   INNER JOIN dbo.Occurrences occ ON p.OccurID = occ.OccurID
								   LEFT JOIN Programs pa ON p.AncestorID = pa.ProgramID
							  WHERE occ.PathID IS NOT NULL
						) prog ON core.ProgID = prog.ProgramID
                   INNER JOIN ( -- segments
						           SELECT  sg.SegmentId AS LogicalSegmentId,
									    sg.SegmentName AS LogicalSegmentName,
									    sg.OccurId AS LogicalSegmentOccurId,
									    db.DBId AS LogicalSegmentDBId,
									    db.DBName AS LogicalSegmentDBName,
									    db.AccessTypeId AS LogicalSegmentDBAccessTypeId,
									    db.OccurId AS LogicalSegmentDBOccurId,
									    srcsg.SegmentID AS PhysicalSegmentId,
									    srcsg.SegmentName AS PhysicalSegmentName,
									    srcsg.SegmentOccurId AS PhysicalSegmentOccurId,
									    srcsg.DBID AS PhysicalSegmentDBId,
									    srcsg.DBName AS PhysicalSegmentDBName,
									    srcsg.AccessTypeId AS PhysicalSegmentDBAccessTypeId,
									    srcsg.DBOccurId AS PhysicalSegmentDBOccurId
								 FROM dbo.IMSDBSegments sg
									  INNER JOIN dbo.IMSDBInfo db ON db.DBId = sg.DBId
									  LEFT OUTER JOIN  -- info related to source segment
												 ( SELECT sg.SegmentID, sg.SegmentName, sg.OccurId AS SegmentOccurID,
												          sg.DBID, db.DBName, db.AccessTypeId, db.OccurId AS DBOccurId
												   FROM dbo.IMSDBSegments sg
													   INNER JOIN dbo.IMSDBInfo db ON db.DBId = sg.DBId
											      ) srcsg ON sg.SourceSegId = srcsg.SegmentID
                                         WHERE  db.AccessTypeId = 13 -- only Logical Segments
						  ) sgmt ON core.ResourceID = sgmt.LogicalSegmentId
						            LEFT OUTER JOIN dbo.Occurrences AS occ_L_SG ON occ_L_SG.OccurID = sgmt.LogicalSegmentOccurId
								  LEFT OUTER JOIN dbo.Occurrences AS occ_L_DB ON occ_L_DB.OccurID = sgmt.LogicalSegmentDBOccurId
								  LEFT OUTER JOIN dbo.Occurrences AS occ_P_SG ON occ_P_SG.OccurID = sgmt.PhysicalSegmentOccurId
								  LEFT OUTER JOIN dbo.Occurrences AS occ_P_DB ON occ_P_DB.OccurID = sgmt.PhysicalSegmentDBOccurId
			      LEFT OUTER JOIN ( -- search fields
							  	 SELECT
							  		   sr.OccurId AS StmtOccurId,
							  		   f.FieldId,
							  		   f.FieldName,
							  		   f.SegmentId,
							  		   f.Seq,
									   os.PathID,
									   os.StartRow,
									   os.StartCol,
									   os.EndRow,
									   os.EndCol
								 FROM dbo.StatementReference sr
										 INNER JOIN dbo.IMSDBFields f on sr.ResourceID = f.FieldId and sr.ResourceType = 222  -- IMS SSA search field
										 LEFT OUTER JOIN dbo.Occurrences os ON f.OccurID = os.OccurID
			    		  ) field on core.OccurId = field.StmtOccurId AND field.SegmentId = sgmt.PhysicalSegmentId

						  GROUP BY   sgmt.LogicalSegmentId, sgmt.LogicalSegmentName,
						             occ_L_SG.StartRow, occ_L_SG.StartCol, occ_L_SG.EndRow, occ_L_SG.EndCol,  occ_L_SG.PathID,
						             sgmt.LogicalSegmentDBId, sgmt.LogicalSegmentDBName, sgmt.LogicalSegmentDBAccessTypeId,
								   occ_L_DB.StartRow, occ_L_DB.StartCol, occ_L_DB.EndRow, occ_L_DB.EndCol, occ_L_DB.PathID,
								   sgmt.PhysicalSegmentId, sgmt.PhysicalSegmentName,
								   occ_P_SG.StartRow, occ_P_SG.StartCol, occ_P_SG.EndRow, occ_P_SG.EndCol,  occ_P_SG.PathID,
								   sgmt.PhysicalSegmentDBId, sgmt.PhysicalSegmentDBName, sgmt.PhysicalSegmentDBAccessTypeId,
								   occ_P_DB.StartRow, occ_P_DB.StartCol, occ_P_DB.EndRow, occ_P_DB.EndCol, occ_P_DB.PathID,
                                           core.StatementTypeId, core.Description,  core.StartRow,  core.StartCol, core.EndRow, core.EndCol, core.PathID,
                                           prog.ProgramName, prog.ProgramID, prog.ProgramTypeID, prog.PathID, prog.StartRow, prog.StartCol, prog.EndRow, prog.EndCol, prog.Ancestor, prog.AncestorID, prog.AncestorTypeID,
                                           field.fieldID, field.fieldName, field.PathID, field.StartRow, field.StartCol, field.EndRow, field.EndCol, field.Seq

		  )c
		   LEFT OUTER JOIN  dbo.Paths pth1 ON pth1.PathID = c.LogicalSegmentDBPathID
		   LEFT OUTER JOIN  dbo.Paths pth2 ON pth2.PathID = c.PhysicalSegmentDBPathID 	
		   LEFT OUTER JOIN  dbo.Paths pth3 ON pth3.PathID = c.StmtPathID
		   LEFT OUTER JOIN  dbo.Paths pth4 ON pth4.PathID = c.ProgramPathID	
		   LEFT OUTER JOIN  dbo.Paths pth5 ON pth5.PathID = c.FieldPathID
		   		

END