日期:2014-05-18 浏览次数:20574 次
declare @t table (AA int,BB varchar(1),CC varchar(1)) insert into @t select 1,'a','A' union all select 1,'a','B' union all select 1,'b','A' union all select 1,'b','C' union all select 2,'a','C' union all select 2,'a','B' SELECT * FROM @t SELECT AA,a=ISNULL(a,'-'),b=ISNULL(b,'-'),c=ISNULL(c,'-') FROM (SELECT *,dd=bb FROM @t) a PIVOT (MAX(dd) FOR CC IN ([A],[B],[C])) b ORDER BY AA /* AA a b c 1 a a - 1 b - b 2 - a a */