Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

Personally I'd do this in the database.

SQL
SELECT Name,SUM(Duration) AS Duration
FROM SomeTable
GROUP BY Name
 
Share this answer
 
Comments
Member 8642100 25-Mar-12 23:55pm    
I got this error..The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect.

i also tried
SELECT [Name], (SUM[Duration] AS Value) from tbWorker group by [Name]

this will return me this error:-
Syntax error (missing operator) in query expression '(SUM[Duration] AS Value)'.
I'm using access database
Ashish Bhujbal 26-Mar-12 0:31am    
Try this:

SELECT [Name], SUM(Duration) AS Value from tbWorker group by [Name]
Member 8642100 26-Mar-12 1:38am    
Hi,
thanks for you reply but i've also tried that and the error is
The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect.
i'm using oledb to connect to access database
C#
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();
 
Share this answer
 
Comments
Member 10227969 21-Feb-14 4:55am    
i have data table with coloum repeated values. but i need distinct values and save as new data table without affecting the old data-table

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900