DECLARE @sq NVARCHAR(4000)
SELECT @sq = ISNULL(@sq + ',', '') + QUOTENAME(SubjectName)
FROM (select distinct SubjectName from T_db_DirectCost) as A
SET @sql='
select r.* from (select ContractBillNo,ContractName,EntrustDepartmentname,SubjectName,CurrentAmountMDr
from T_db_DirectCost where CurrentAmountMDr is not null) as t
pivot(max(t.CurrentAmountMDr)for t.SubjectName in ('+@sq+')) as r where ContractBillNo is not null'
EXEC( @sql)
行转列
最新推荐文章于 2024-09-06 12:25:21 发布