问题描述
大家好,
我在这里有一个小问题.我有一个具有4列的访问数据库,即名称","duration(mins)",电子邮件".名称列的值将重复.所以我想要的是始终将相似的事物和加法的事物结合在一起.例如:-
旧数据库
姓名|持续时间
詹姆斯| 30
莎拉| 45
约翰| 100
詹姆斯| 40
詹姆斯| 90
莎拉| 10
新数据库
名称持续时间
詹姆斯| 170
莎拉| 55
约翰| 100
对于旧数据库,我设法将所有内容放入数据表中.但是从那里,我陷入了困境.我不确定如何找到重复的值并添加数字.请帮助我.我正在使用c#.
谢谢.
Hello Everyone,
I''m having a little problem here. I have a database in access with 4 columns namely, "name", "duration(mins)" , "email". Name column value will be repeated. So what i want is to combine the similar and the plus all the time together. for example:-
old database
Name | duration
James | 30
Sarah | 45
John | 100
James | 40
James | 90
Sarah | 10
new database
Name duration
James | 170
Sarah | 55
John | 100
for the old database, I manage to put everything into a datatable. However from there, I''m stuck.. I''m not sure how do i find the duplicate values and add the numbers.Help me please. I''m using c#.
Thank you.
推荐答案
SELECT Name,SUM(Duration) AS Duration
FROM SomeTable
GROUP BY Name
foreach (DataRow row in dt.Rows)
{
string name = row["Name"].ToString();
int sumDuration = Convert.ToInt32(row["Duration"]);
if (dicSum.ContainsKey(name))
dicSum[name] += sumDuration;
else
dicSum.Add(name, sumDuration);
}
foreach (string i in dicSum.Keys)
Console.WriteLine("{0}={1}", i, dicSum[i]);
Console.ReadLine();
这篇关于在一列中搜索重复值并进行加法(数据表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!