我使用动态SQL来运行查询:
select @query = '
SELECT C.LdcCode, ' + @cols + '
FROM DailyTimesheetUnprocessedCount AS C
LEFT OUTER JOIN #LdcSorting AS S
ON C.LdcCode = S.LdcCode
GROUP BY C.LdcCode, S.SortOrder
ORDER BY ISNULL(S.SortOrder, 100)
'
execute(@query)
是否有一种简单的方法来转置查询结果,所以在我的情况下,LdcCodes将是列名,而@cols中现在的内容将是第一列中的值?
感谢。