You can group by Cattle
SELECT Cattle, SUM(Liter) AS TotalLiter
FROM NewMilk
GROUP BY Cattle
ORDER BY Cattle
Or if you need the total of only one specific cattle type
SELECT SUM(Liter) AS TotalLiter
FROM NewMilk
WHERE Cattle="Cow"
You can execute a SQL command returning rows like this
string sql = "SELECT Cattle, SUM(Liter) AS TotalLiter ... "; // 1st SQL example.
using (var con = new SqlConnection("connection string"))
using (var cmd = new SqlCommand(sql, con)) {
con.Open();
var reader = cmd.ExecuteReader();
while (reader.Read()) {
string cattle = reader.GetString(0); // 1st column
int total = reader.GetInt32(1); // 2nd column
...
}
}
Or, if you have only one row with one column, i.e. one result value
string sql = "SELECT SUM(Liter) AS TotalLiter ... "; // 2nd SQL example.
using (var con = new SqlConnection("connection string"))
using (var cmd = new SqlCommand(sql, con)) {
con.Open();
int total = (int)cmd.ExecuteScalar();
...
}
See also: The Right Way to Query a Database: Parameterizing your SQL Queries.
3
solved How to select the SQL database values that has the same name and sum up their corresponded values [closed]