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)
sqlserver 行转列
最新推荐文章于 2024-12-07 21:24:49 发布