循环生成表名代码
1 if (startYM == endYM)
2 {
3 tableName1 = "ANALOG_DATA_" + startYM; //表名
4 sql = "select min(pval),max(pval),round(avg(pval),5) from "+tableName1+" where pval>0 and pid='" + pid + "'and dtag>=" + dtagStart + " and dtag<=" + dtagEnd + "";
5 }
6 else
7 {
8 monthCount = 12 * (endYear - startYear) + (endMonth - startMonth) + 1;
9 for (int monthNum = 0; monthNum < monthCount; monthNum++)
10 {
11 tableName1 = "ANALOG_DATA_" + startDate.AddMonths(monthNum).ToString("yyyyMMdd").Substring(0, 6);
12 if (monthNum >= 0 && monthNum<monthCount-1)
13 {
14 sbSql.Append("select min(pval) as pvalMin,max(pval) as pvalMax,round(avg(pval),5) as pvalAvg from "+tableName1+" where pval>0 and pid='" + pid + "'and dtag>=" + dtagStart + " and dtag<=" + dtagEnd + " union ");
15 }
16 else //最后一个月数据
17 {
18 sbSql.Append("select min(pval) as pvalMin,max(pval) as pvalMax,round(avg(pval),5) as pvalAvg from " + tableName1 + " where pval>0 and pid='" + pid + "'and dtag>=" + dtagStart + " and dtag<=" + dtagEnd + "");
19 }
20 }
21 sql = "select min(pvalMin),max(pvalMax),round(avg(pvalAvg)) from (" + sbSql.ToString() + ")";
22 }
2 {
3 tableName1 = "ANALOG_DATA_" + startYM; //表名
4 sql = "select min(pval),max(pval),round(avg(pval),5) from "+tableName1+" where pval>0 and pid='" + pid + "'and dtag>=" + dtagStart + " and dtag<=" + dtagEnd + "";
5 }
6 else
7 {
8 monthCount = 12 * (endYear - startYear) + (endMonth - startMonth) + 1;
9 for (int monthNum = 0; monthNum < monthCount; monthNum++)
10 {
11 tableName1 = "ANALOG_DATA_" + startDate.AddMonths(monthNum).ToString("yyyyMMdd").Substring(0, 6);
12 if (monthNum >= 0 && monthNum<monthCount-1)
13 {
14 sbSql.Append("select min(pval) as pvalMin,max(pval) as pvalMax,round(avg(pval),5) as pvalAvg from "+tableName1+" where pval>0 and pid='" + pid + "'and dtag>=" + dtagStart + " and dtag<=" + dtagEnd + " union ");
15 }
16 else //最后一个月数据
17 {
18 sbSql.Append("select min(pval) as pvalMin,max(pval) as pvalMax,round(avg(pval),5) as pvalAvg from " + tableName1 + " where pval>0 and pid='" + pid + "'and dtag>=" + dtagStart + " and dtag<=" + dtagEnd + "");
19 }
20 }
21 sql = "select min(pvalMin),max(pvalMax),round(avg(pvalAvg)) from (" + sbSql.ToString() + ")";
22 }