Dear Friends ,
I had a situation , Like i wanna to sum up two datatable same column...
i shown the data and desired data .. If anybody Possible ..Guide me
| Datatable :1 | ||
| Sno | Agent Id | Amount |
| 1 | AG121 | 400 |
| 2 | AG125 | 300 |
| 3 | AG127 | 200 |
| Datatable :2 | ||
| Sno | Agent Id | Amount |
| 1 | AG121 | 100 |
| 2 | AG125 | 200 |
| 3 | AG127 | 100 |
| 4 | AG129 | 400 |
My desired OutPut Is,
SNo Agent Id Amount
1 AG121 500
2 AG125 500
3 AG127 300
4 AG129 400
This what i am really want to do ...
Thanks in advance
- KARTHIK

Sanjeeb LenkaPosted Oct 8, 2013, 7:50 AM
you can do this using linq
check this sample code:
DataTable dt = new DataTable();
dt.Columns.Add("sno", typeof(int));
dt.Columns.Add("AgentId", typeof(string));
dt.Columns.Add("Amount", typeof(int));
dt.Rows.Add(1, "AG121", 400);
dt.Rows.Add(2, "AG125", 300);
dt.Rows.Add(3, "AG127", 200);
DataTable dt2 = new DataTable();
dt2.Columns.Add("sno", typeof(int));
dt2.Columns.Add("AgentId", typeof(string));
dt2.Columns.Add("Amount", typeof(int));
dt2.Rows.Add(1, "AG121", 100);
dt2.Rows.Add(2, "AG125", 200);
dt2.Rows.Add(3, "AG127", 100);
dt2.Rows.Add(3, "AG129", 400);
var result = from t1 in dt2.AsEnumerable()
join t2 in dt.AsEnumerable() on t1["AgentId"] equals t2["AgentId"] into ds
from o in ds.DefaultIfEmpty(null)
select new
{
Sno = t1["sno"],
AgentId = t1["AgentId"],
Amount = (int?)((t1 == null) ? 0 : t1["Amount"]) + (int?)((o == null) ? 0 : o["Amount"]),
};
DataTable dt3 = new DataTable();
dt3.Columns.Add("sno", typeof(string));
dt3.Columns.Add("AgentId", typeof(string));
dt3.Columns.Add("Amount", typeof(string));
foreach (var item in result)
{
dt3.Rows.Add(item.Sno.ToString(), item.AgentId.ToString(), item.Amount.ToString());
}
Jignesh TrivediPosted Oct 8, 2013, 6:00 AM
hi,
Is your two table is in different database and server?
if yes I think database link can help you.
Please read
http://docs.oracle.com/cd/B28359_01/server.111/b28310/ds_concepts002.htm
hope this will help you.
Prasenjit DeyPosted Oct 8, 2013, 4:49 AM
venkata kumarPosted Oct 8, 2013, 4:18 AM
try the fallowing
select t.AgentID,sum(t.amount+t1.amount) from databasename1.dbo.tablename t inner join databasename2.dbo.tablename t1 ON t1.AgentID=t.AgentID
group by t. AgentID,t.amount
Karthik KPosted Oct 8, 2013, 3:59 AM
I Have using the Oracle Database.. Two Databases and i had permision,
I havent use it ..i dont know how to joint two different database table in one query,
can you say one example for...
this is very new concepts for me ..
Thanks
KARTHIK
Prasenjit DeyPosted Oct 8, 2013, 3:51 AM
Karthik KPosted Oct 8, 2013, 3:32 AM
If it same db i can able to do as your way , i had those data in different database
So i unable to do in that way , Provide me other option to complete that one
Thanks
KARTHIK
venkata kumarPosted Oct 8, 2013, 3:13 AM
try the fallowing
select t.Agent Id,sum(t.amount+t1.amount) from table_1 t inner join table_2 t1 ON t1. Agent Id=t. Agent Id
group by t. Agent Id,t.amount