SET @sql = 'SELECT DISTINCT dbo.ISS_Base_Commodity.Name [商品] ,SUM(Numer) [总销量]'
SELECT @sql = @sql
+ ' ,CAST(ROUND(CAST(SUM(CASE dbo.ISS_System_Province.Name when '''
+ [省份]
+ ''' THEN Numer ELSE 0 END) AS FLOAT) / CAST(SUM(Numer) AS FLOAT), 4) * 100 AS VARCHAR(20))+''%'' ['
+ [省份] + ']'
FROM ( SELECT DISTINCT
Name [省份] ,
PKID
FROM dbo.ISS_System_Province
GROUP BY PKID ,
Name
) a
SET @sql = @sql
+ 'FROM ISS_Base_Commodity
INNER JOIN ISS_Main_OrderInfo ON ISS_Base_Commodity.PKID = ISS_Main_OrderInfo.ISS_Base_Commodity_PKID
INNER JOIN ISS_Base_Customer ON ISS_Main_OrderInfo.ISS_Base_Customer_PKID = ISS_Base_Customer.PKID
INNER JOIN ISS_System_Province ON ISS_Base_Customer.ISS_System_Province = ISS_System_Province.PKID
GROUP BY ISS_Base_Commodity.Name'
PRINT @sql
EXEC(@sql)