DROP TABLE #tb create table #tb([品号] int,[日期] varchar(5),[数量] int) insert #tb select 1001,'06-08',11 union all select 1001,'06-09',12 union all select 1001,'06-10',13 union all select 1001,'06-11',14 union all select 1001,'06-12',15
DECLARE @sql NVARCHAR(MAX), @colList NVARCHAR(MAX) SET @colList = STUFF((SELECT ','+QUOTENAME([日期]) FROM #tb GROUP BY [日期] ORDER BY [日期] FOR XML PATH('')),1,1,'') SET @sql = N' select * from #tb a pivot (max([数量]) for [日期] in('+ @colList +')) b ' EXEC(@sql) /* 品号06-0806-0906-1006-1106-12 10011112131415 */