as select ProgramName from (
select distinct 
	case when Value is null then DDCLElement.Name
	else Value
	end	as ProgramName
	FROM DDCLElement 
	left join DDCLProperty p on p.ElementId = DDCLElement.Id and p.Name IN ('PROGRAM-ID')
	left join DDCLValue ON DDCLValue.PropertyId = p.Id and DDCLValue.ValueTypeId = 1 and Value is not null and Value != ''
	where DdsTypeId IN (27, 28)
union
select distinct
	DDCLValue.Value as ProgramName
	FROM DDCLElement 
	left join DDCLProperty p on p.ElementId = DDCLElement.Id and p.Name IN ('COMPONENT-ID')
	inner join DDCLValue ON DDCLValue.PropertyId = p.Id and DDCLValue.ValueTypeId = 1 and Value is not null
	where DdsTypeId IN (11)
union
select distinct
	ReferenceName as ProgramName
	FROM DDCLElement 
	left join DDCLProperty p on p.ElementId = DDCLElement.Id and p.Name IN ('TASK')
	inner join DDCLValue ON DDCLValue.PropertyId = p.Id and DDCLValue.ValueTypeId in (2, 4)
	where DdsTypeId IN (26)
) as q where ProgramName is not null
