C#SQL查询 - 尝试使用sum(weekly_slot)FROM group by

时间:2017-05-22 02:40:34

标签: c# sql

我想根据weekly_slot dbtable中的Teacher_NameGroupOdd,但是我得到的所有行都有10个值。感谢能否纠正我。除此之外,如何更新GroupOdd dbtable以根据较大的sum_weekly_slot排序?
感谢。

cmd2 = new SQLiteCommand();
cmd2 = dbConnect.CreateCommand();
//cmd2.CommandText = "DELETE FROM GroupOdd";
//cmd2.ExecuteNonQuery();
cmd2.CommandText = "SELECT Sum(Weekly_Slot) AS Sum_Weekly_Slot FROM GroupOdd group by Teacher_Name";
DataTable dt2 = new DataTable();
SQLiteDataAdapter da2 = new SQLiteDataAdapter(cmd2);
da2.Fill(dt2);
foreach (DataRow dr2 in dt2.Rows)
{
    //cmd.CommandText = "INSERT INTO GroupOdd (Teacher_Name, Standard, Subject, Weekly_Slot, Balance_Slot) VALUES (@Teacher_Name, @Standard, @Subject, @Weekly_Slot, @Balance_Slot)";
    cmd2.CommandText = "UPDATE GroupOdd SET Sum_Weekly_Slot = @Sum_Weekly_Slot";
    //cmd2.Parameters.AddWithValue("@Sum_Weekly_Slot", dr2["Sum(Weekly_Slot)"].ToString());
    cmd2.Parameters.AddWithValue("@Sum_Weekly_Slot", dr2["Sum_Weekly_Slot"].ToString());
    cmd2.ExecuteNonQuery();
}

2 个答案:

答案 0 :(得分:0)

您需要的只是具有MAX聚合函数的GROUP BY子句:

SELECT id, MAX(rev)
FROM YourTable
GROUP BY id

相应地更新它。

如果

Sum(Weekly_Slot);

不是你要找的东西

尝试

Count(Weekly_Slot)

答案 1 :(得分:0)

SELECT Teacher_Name, Sum(Weekly_Slot) AS Sum_Weekly_Slot FROM GroupOdd 
group by Teacher_Name order by Sum(Weekly_Slot) desc

检查Teacher_Names是否唯一,否则尝试为教师使用Id(TeacherId)。 Sum(Weekly_Slot) desc的顺序应该首先按表格排序。