declare @s varchar(8000)
set @s=''
select @s=@s+',['+名称+'_数量]=sum(case 名称 when '''
+名称+''' then 数量 else 0 end),['+名称
+'_金额]=sum(case 名称 when '''+名称
+''' then 金额 else 0 end)'
from 基础表
where month(时间)=12  --要查询的月份
group by 名称
exec('select 日=day(时间),金额日汇总=sum(金额),数量日汇总=sum(数量)'+@s+'
from 基础表
where month(时间)=12  --要查询的月份
group by day(时间)
')