楼上的你看着:if object_id('table1') >0 drop table table1create table table1 (商场名称 varchar(100),商品 varchar(100),数量 int )insert into table1 values ('AAA','毛巾',100)insert into table1 values ('AAA','牙刷',50)insert into table1 values ('BBB','毛巾',80)insert into table1 values ('BBB','篮球',10)insert into table1 values ('AAA','毛巾',50)insert into table1 values ('AAA','牙刷',50)insert into table1 values ('BBB','篮球',40)insert into table1 values ('AAA','手机',50)insert into table1 values ('BBB','手机',50)godeclare @sql varchar(5000)declare @goods varchar(100)declare mycur1 cursor for select distinct 商品 from table1 open mycur1 set @sql='select 商场名称,'fetch next from mycur1 into @goods while @@fetch_status=0 begin set @sql=@sql+'sum(case 商品 when '''+@goods+''' then 数量 else 0 end ) as '+@goods+',' fetch next from mycur1 into @goods endset @sql=left(@sql,Len(@sql)-1) --去掉最后一个逗号 set @sql=@sql+' from table1 group by 商场名称 'print @sqlexec(@sql) close mycur1 deallocate mycur1 --以上SQL语句在SQL2005下测试通过