sql += " set @sql = 'select [name] as ''名称'',mbz as ''目标值'' ,yjz as ''预警值'' ,zrr as ''责任人'',sum(type)/count(*) as ''达标占比'',sum(type)/count(*) as ''统计'','";
sql += " select @sql = @sql + 'sum(case Convert(varchar(10),tims,121) when '''+n+''' then value else 0 end) as '''+n+'日'',sum(case Convert(varchar(10),tims,121) when '''+n+''' then id else 0 end) as ''--'','";
sql += " from (select distinct top 32 tims as n from rjk where type=1 and left(timss,len(timss)-10)='2012-9' order by tims desc) as a";
sql += " select @sql = left(@sql,len(@sql)-1) + ' from rjk where type=''1'' and left(timss,len(timss)-10)=''" + str + "'' group by [name],mbz,yjz,zrr'";
sql += " exec(@sql)";
cn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql.ToString(), cn);