答案 0 :(得分:1)
您的dyanmic pivot查询应该与此 demo
一样declare @cols nvarchar(max);
declare @query nvarchar(max);
select
sourceId,
patientId,
data,
cols= concat(col,sortOrder)
into #t
from
Contacts
UNPIVOT
(
data for col in
(
personalContactType_MisContactTypeId,
personalContactNameLast,
personalContactNameFirst
)
)up
select @cols= stuff((
select distinct ','+ quotename(cols)
from #t
for xml path('')),1,1,'')
select @cols
select @query= 'select * from #t
pivot
( max(data) for cols in ('+@cols+
'))p'
exec (@query)
答案 1 :(得分:0)
在dynamic pivot中,在创建列列表时使用val
进行ORDER BY。