把欄位的值用sql語句寫成表頭
Declare @sql varchar(8000)
set @sql = 'select address '
select @sql = @sql + ', MAX(case products when '''+ products+''' then price else null end ) as ['+ products+']'
from (select distinct products from a ) as mycb
select @sql = @sql+' from a group by address'
exec(@sql)
Declare @sql varchar(8000)
set @sql = 'select address'
print @sql
select @sql = @sql + ', MAX(case products when '''+
products+''' then price else null end ) as ['+ products+']'
from (select distinct products from a group by products ) as mycb
print @sql
select @sql = @sql+' from a group by address,products order by address'
print @sql
exec(@sql)