子陌,刚刚的问题还有点小麻烦!
create table test (店面 varchar(10),产品 nvarchar(10),批次 nvarchar(10))
insert into test select 'A店','1',100
insert into test select 'A店','2',200
insert into test select 'A店','3',100
insert into test select 'B店','1','a154'
insert into test select 'B店','3',200
insert into test select 'C店','3',200
go
select * from test
declare @sql varchar(8000)
set @sql='select 店面'
select @sql=@sql+',['+rtrim(产品)+']=max(case 产品 when '+rtrim(产品)+' then 批次 else 0 end)'
from test group by 产品 order by 产品
set @sql=@sql+' from test group by 店面'
exec(@sql)
go
drop table test
go
------------------------------------------
提示是出错的