AS
     SELECT DISTINCT
            dde.Id,
            dde.Name,
            dde.DdsTypeId,
            DDCLElementChild.Id AS ChildId,
            ElementSourceInfo.StartRow AS ElementStartRow,
            ElementSourceInfo.EndRow AS ElementEndRow,
            Paths_1.PathStr AS ElementPathStr,
            CallSourceInfo.StartRow AS CallStartRow,
            CallSourceInfo.EndRow AS CallEndRow,
            Paths_2.PathStr AS CallPathStr,
            ddp.Name AS PropertyName
     FROM DDCLElement dde
          INNER JOIN DDCLValue ddv ON dde.Id = ddv.ElementId
          INNER JOIN DDCLProperty ddp ON ddv.PropertyId = ddp.Id
          INNER JOIN DDCLElement AS DDCLElementChild ON ddv.ReferenceName = DDCLElementChild.Name
                                                            AND ddv.ReferenceDdsTypeId = DDCLElementChild.DdsTypeId
          INNER JOIN DDCLSourceInfo AS ElementSourceInfo ON dde.SourceInfo = ElementSourceInfo.Id
          INNER JOIN Programs AS Programs_1 ON ElementSourceInfo.ProgramId = Programs_1.ProgramID
          INNER JOIN Occurrences AS Occurrences_1 ON Programs_1.OccurID = Occurrences_1.OccurID
          INNER JOIN Paths AS Paths_1 ON Occurrences_1.PathID = Paths_1.PathID
          INNER JOIN DDCLSourceInfo AS CallSourceInfo ON ddv.SourceInfo = CallSourceInfo.Id
          INNER JOIN Programs AS Programs_2 ON CallSourceInfo.ProgramId = Programs_2.ProgramID
          INNER JOIN Occurrences AS Occurrences_2 ON Programs_2.OccurID = Occurrences_2.OccurID
          INNER JOIN Paths AS Paths_2 ON Occurrences_2.PathID = Paths_2.PathID
          INNER JOIN DDCLRelationV AS DDCLRelationV_Parent ON dde.Id = DDCLRelationV_Parent.FromElementId
          INNER JOIN DDCLElement AS SubschemaElem ON DDCLRelationV_Parent.ToElementId = SubschemaElem.Id
		-- next joins are implemented for filter purpose
		LEFT OUTER JOIN (
						  select ssp.Id, ssp.ElementId, rrv.ReferenceName, rrv.ReferenceDdsTypeId 
						  from DDCLProperty ssp
								inner join DDCLValue ssv on ssp.id = ssv.PropertyId and ssv.ValueTypeId = 1 and ssv.Name = 'ALL' and ssv.Value = 'TRUE'
								inner join DDCLValue srv on ssp.id = srv.PropertyId and srv.ValueTypeId = 2 and srv.ReferenceDdsTypeId = 6
								inner join DDCLElement se on se.Name = srv.ReferenceName and se.DdsTypeId = 6
								inner join DDCLValue rrv on rrv.ElementId = se.Id and rrv.ReferenceDdsTypeId = 10
						  where  ssp.Name =  'SCHEMA' 				  
						  )f1 ON f1.ElementId = SubschemaElem.Id AND  f1.ReferenceName = ddv.ReferenceName 
		LEFT OUTER JOIN (
							 select ssp.Id, ssp.ElementId , ssv.ReferenceName, ssav.Id AS ssavID, ssp.Name , ssv.ReferenceDdsTypeId 
							 from DDCLProperty ssp
								    left join DDCLValue ssv on ssp.id = ssv.PropertyId and ssv.ValueTypeId = 2 
								    left join DDCLValue ssav on ssp.id = ssav.PropertyId  and ssav.ValueTypeId = 1 and ssav.Name = 'ALL' and ssav.Value = 'TRUE'
							 where ssp.Name  = 'SETS'
						  )f2 ON  f2.ElementId = SubschemaElem.Id
								AND f2.ReferenceDdsTypeId = ddv.ReferenceDdsTypeId
								AND (f2.ReferenceName = ddv.ReferenceName or f2.ssavID is not null)	
     WHERE DDCLElementChild.Id IN ( SELECT * FROM #numeric_param_temp ) 
     	   AND ddv.ValueTypeId IN (2, 4)
           AND ddv.ReferenceDdsTypeId = 10
           AND dde.DdsTypeId IN (11, 16) 
		   AND (f1.ID IS NOT NULL OR f2.ID IS NOT NULL)
		   AND ddp.name IN ('CHANGES', 'SINGULAR-VIEW')
     ORDER BY dde.Name;