你可以考虑 unpivot
declare @t table(id int identity(1,1), Property1 char(1),
Property2 char(1),
Property3 char(1),
Property4 char(1),
Property5 char(1))
insert @t values('N', 'Y', 'Y', 'N', 'Y')
insert @t values('N', 'N', 'Y', 'N', 'Y')
insert @t values('N', 'N', 'N', 'N', 'Y')
;with a as
(
select *, row_number() over (partition by id order by id) position from @t
unpivot
(Property FOR colname IN
([Property1], [Property2], [Property3], [Property4],
[Property5]/*include more properties here*/) ) AS unpvt
)
select t.id, coalesce(colname, 'Not found') colname
from @t t
outer apply
(select top 1 id, colname, position
from a where Property = 'Y'
and t.id = id
order by id
) x