我创建了函数分析学生成绩表。目的是:找到按内容类型分组的最高平均结果。例如:
S_Id ContentType Result
1 T 50
1 V 70
1 G 30
1 G 40
1 V 60
我需要的输出是 V,因为 V 的平均值最高。
这是我的代码,但它没有显示正确的结果: 我的输出显示表中的第五行。
public string analyzeResultTable(string studentId)
{
string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["AHSConnection"].ToString();
DataSet ds = new DataSet();
DataSet dsAns = new DataSet();
string BestPrefrence = "";
using (MySqlConnection conn = new MySqlConnection(connStr))
{
conn.Open();
MySqlCommand cmd2 =
new MySqlCommand(
"Select ContentType, MAX(avgContent) from (select ContentType, AVG(result) as avgContent from dopractice where S_Id='" +
studentId + "' GROUP BY ContentType) AS T", conn);
MySqlDataAdapter da = new MySqlDataAdapter(cmd2);
da.Fill(ds);
conn.Close();
}
if (ds.Tables[0].Rows.Count > 0)
{
BestPrefrence = ds.Tables[0].Rows[0]["ContentType"].ToString();
}
return BestPrefrence;
}
最佳答案
这将为您提供每个 ContentType 的平均结果
select ContentType, AVG(result) as avgContent from dopractice where S_Id='" + studentId + "' GROUP BY ContentType order by AVG(result) desc limit 1;
添加订单后您将得到您想要的
关于C#:Select 语句未出现正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38149026/