Declare @Component smallint = -1 Select sc.Comment As Comment, t.NameFull As CommentType, sc.parenttypeId As Component, dt.periodQuarter, dt.periodYearFrom srcComment sc inner JOIN dimPeriod p ON srcComment.ParentId = p.periodId Left Outer Join refType t ON t.TypeId = srcComment.ParentTypeId outer apply (select Quarter, Year from dimPeriod dp where dp.periodId = sc.ParentId)dt(periodQuarter, periodYear)where (@Component = -1 and sc.parenttypeId IN (601, 605, 606)) or (sc.parenttypeId = @Component);