日期:2014-05-16 浏览次数:20491 次
DECLARE @sql NVARCHAR(4000)
SELECT @sql = ISNULL(@sql , '') + N' sum(case when 客户 = N''' + 客户 + ''' then 数量 else 0 end) as [' +客户+N'|数量1]'
+N' , sum(case when 客户 = N''' + 客户 + N''' then 金额 else 0 end) as ['+客户+N'|金额1]'+ N',' FROM t2
SET @sql = 'SELECT t3.编号 , t3.产品名称,'+ left(@sql , LEN(@sql) - 1 ) + N', ISNULL(sum(数量),0) as [合计|数量] , ISNULL(sum(金额),0) as [合计|金额] from t1 right join t3 on t1.编号 = t3.编号 group by t3.编号,t3.产品名称 order by t3.编号'
PRINT @sql
EXEC(@sql)