473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Joining DataTables

Is there anyway of Joining two or more DataTable with similar structure?

I have three DataTables with following structures

Data, AmountB/F, Repayments, InterestCharged and AmountC/F

i want to join these tables on the basis of Date. I don't want three rows for each date, i want the values to be added for similar dates.

thanx

Jul 21 '05 #1
4 4230
Depending on the size of the tables, this operation (which is not a join...
it's a merge with roll-up), can take an extraordinarily long period of time.

Regardless, take a look at the syntax for the UNION keyword in the SELECT
statement.
http://msdn.microsoft.com/library/de...a-ses_9sfo.asp

Otherwise knows as a UNION Query, you can combine the results from a number
of queries and then group by specific columns.

Normally, this kind of functionality is performed in an ETL operation on a
periodic basis (often daily) into a data cube and simply queried from there
using MDX. If you are not familiar with SQL Analysis Server, you may want
to take a look.

--- Nick

"Job Lot" <Jo****@discussions.microsoft.com> wrote in message
news:2D**********************************@microsof t.com...
Is there anyway of Joining two or more DataTable with similar structure?

I have three DataTables with following structures

Data, AmountB/F, Repayments, InterestCharged and AmountC/F

i want to join these tables on the basis of Date. I don't want three rows for each date, i want the values to be added for similar dates.
thanx

Jul 21 '05 #2
Job Lot,
It sounds like you want to update one DataTable with the values of a second
DataTable, rather then doing a database "Join" or a database "Union".

You will need to manually do this, with a loop similar to:

Public Shared Sub Main()
Dim ds As New DataSet("Job Lot")
Dim table1 As DataTable = GetTable("table1")
Dim table2 As DataTable = GetTable("table2")
Dim table3 As DataTable = GetTable("table3")
ds.Tables.Add(table1)
ds.Tables.Add(table2)
ds.Tables.Add(table3)
AddTable(table1, table2)
AddTable(table1, table3)
End Sub

Private Shared Sub AddTable(ByVal mainTable As DataTable, ByVal
sourceTable As DataTable)
For Each sourceRow As DataRow In sourceTable.Rows
If mainTable.Rows.Contains(sourceRow("Data")) Then
Dim mainRow As DataRow = mainTable.Rows.Find(sourceRow!Data)
mainRow.BeginEdit()
mainRow("AmountB/F") = CDec(mainRow("AmountB/F")) +
CDec(sourceRow("AmountB/F"))
mainRow("Repayments") = CDec(mainRow("Repayments")) +
CDec(sourceRow("Repayments"))
mainRow("InterestCharged") =
CDec(mainRow("InterestCharged")) + CDec(sourceRow("InterestCharged"))
mainRow("AmountC/F") = CDec(mainRow("AmountC/F")) +
CDec(sourceRow("AmountC/F"))
mainRow.EndEdit()
Else
mainTable.ImportRow(sourceRow)
End If
Next
End Sub

Private Shared Function GetTable(ByVal tableName As String) As DataTable
Dim table As New DataTable(tableName)
With table.Columns
.Add("Data", GetType(DateTime))
.Add("AmountB/F", GetType(Decimal))
.Add("Repayments", GetType(Decimal))
.Add("InterestCharged", GetType(Decimal))
.Add("AmountC/F", GetType(Decimal))
End With
table.PrimaryKey = New DataColumn() {table.Columns("Data")}
With table.Rows
.Add(New Object() {#1/1/2004#, 10, 20, 30, 40})
.Add(New Object() {#4/1/2004#, 10, 20, 30, 40})
.Add(New Object() {#7/1/2004#, 10, 20, 30, 40})
End With
Return table
End Function

Hope this helps
Jay

"Job Lot" <Jo****@discussions.microsoft.com> wrote in message
news:2D**********************************@microsof t.com...
Is there anyway of Joining two or more DataTable with similar structure?

I have three DataTables with following structures

Data, AmountB/F, Repayments, InterestCharged and AmountC/F

i want to join these tables on the basis of Date. I don't want three rows for each date, i want the values to be added for similar dates.
thanx

Jul 21 '05 #3
Hi Jay,

thanks for the code. i am getting an error saying "Table does not have a primary key", whereas i have created pk column in my datatable??? :(

"Jay B. Harlow [MVP - Outlook]" wrote:
Job Lot,
It sounds like you want to update one DataTable with the values of a second
DataTable, rather then doing a database "Join" or a database "Union".

You will need to manually do this, with a loop similar to:

Public Shared Sub Main()
Dim ds As New DataSet("Job Lot")
Dim table1 As DataTable = GetTable("table1")
Dim table2 As DataTable = GetTable("table2")
Dim table3 As DataTable = GetTable("table3")
ds.Tables.Add(table1)
ds.Tables.Add(table2)
ds.Tables.Add(table3)
AddTable(table1, table2)
AddTable(table1, table3)
End Sub

Private Shared Sub AddTable(ByVal mainTable As DataTable, ByVal
sourceTable As DataTable)
For Each sourceRow As DataRow In sourceTable.Rows
If mainTable.Rows.Contains(sourceRow("Data")) Then
Dim mainRow As DataRow = mainTable.Rows.Find(sourceRow!Data)
mainRow.BeginEdit()
mainRow("AmountB/F") = CDec(mainRow("AmountB/F")) +
CDec(sourceRow("AmountB/F"))
mainRow("Repayments") = CDec(mainRow("Repayments")) +
CDec(sourceRow("Repayments"))
mainRow("InterestCharged") =
CDec(mainRow("InterestCharged")) + CDec(sourceRow("InterestCharged"))
mainRow("AmountC/F") = CDec(mainRow("AmountC/F")) +
CDec(sourceRow("AmountC/F"))
mainRow.EndEdit()
Else
mainTable.ImportRow(sourceRow)
End If
Next
End Sub

Private Shared Function GetTable(ByVal tableName As String) As DataTable
Dim table As New DataTable(tableName)
With table.Columns
.Add("Data", GetType(DateTime))
.Add("AmountB/F", GetType(Decimal))
.Add("Repayments", GetType(Decimal))
.Add("InterestCharged", GetType(Decimal))
.Add("AmountC/F", GetType(Decimal))
End With
table.PrimaryKey = New DataColumn() {table.Columns("Data")}
With table.Rows
.Add(New Object() {#1/1/2004#, 10, 20, 30, 40})
.Add(New Object() {#4/1/2004#, 10, 20, 30, 40})
.Add(New Object() {#7/1/2004#, 10, 20, 30, 40})
End With
Return table
End Function

Hope this helps
Jay

"Job Lot" <Jo****@discussions.microsoft.com> wrote in message
news:2D**********************************@microsof t.com...
Is there anyway of Joining two or more DataTable with similar structure?

I have three DataTables with following structures

Data, AmountB/F, Repayments, InterestCharged and AmountC/F

i want to join these tables on the basis of Date. I don't want three rows

for each date, i want the values to be added for similar dates.

thanx


Jul 21 '05 #4
I found the error in my code. Thanx a ton for your help

"Jay B. Harlow [MVP - Outlook]" wrote:
Job Lot,
It sounds like you want to update one DataTable with the values of a second
DataTable, rather then doing a database "Join" or a database "Union".

You will need to manually do this, with a loop similar to:

Public Shared Sub Main()
Dim ds As New DataSet("Job Lot")
Dim table1 As DataTable = GetTable("table1")
Dim table2 As DataTable = GetTable("table2")
Dim table3 As DataTable = GetTable("table3")
ds.Tables.Add(table1)
ds.Tables.Add(table2)
ds.Tables.Add(table3)
AddTable(table1, table2)
AddTable(table1, table3)
End Sub

Private Shared Sub AddTable(ByVal mainTable As DataTable, ByVal
sourceTable As DataTable)
For Each sourceRow As DataRow In sourceTable.Rows
If mainTable.Rows.Contains(sourceRow("Data")) Then
Dim mainRow As DataRow = mainTable.Rows.Find(sourceRow!Data)
mainRow.BeginEdit()
mainRow("AmountB/F") = CDec(mainRow("AmountB/F")) +
CDec(sourceRow("AmountB/F"))
mainRow("Repayments") = CDec(mainRow("Repayments")) +
CDec(sourceRow("Repayments"))
mainRow("InterestCharged") =
CDec(mainRow("InterestCharged")) + CDec(sourceRow("InterestCharged"))
mainRow("AmountC/F") = CDec(mainRow("AmountC/F")) +
CDec(sourceRow("AmountC/F"))
mainRow.EndEdit()
Else
mainTable.ImportRow(sourceRow)
End If
Next
End Sub

Private Shared Function GetTable(ByVal tableName As String) As DataTable
Dim table As New DataTable(tableName)
With table.Columns
.Add("Data", GetType(DateTime))
.Add("AmountB/F", GetType(Decimal))
.Add("Repayments", GetType(Decimal))
.Add("InterestCharged", GetType(Decimal))
.Add("AmountC/F", GetType(Decimal))
End With
table.PrimaryKey = New DataColumn() {table.Columns("Data")}
With table.Rows
.Add(New Object() {#1/1/2004#, 10, 20, 30, 40})
.Add(New Object() {#4/1/2004#, 10, 20, 30, 40})
.Add(New Object() {#7/1/2004#, 10, 20, 30, 40})
End With
Return table
End Function

Hope this helps
Jay

"Job Lot" <Jo****@discussions.microsoft.com> wrote in message
news:2D**********************************@microsof t.com...
Is there anyway of Joining two or more DataTable with similar structure?

I have three DataTables with following structures

Data, AmountB/F, Repayments, InterestCharged and AmountC/F

i want to join these tables on the basis of Date. I don't want three rows

for each date, i want the values to be added for similar dates.

thanx


Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jade | last post by:
Hi, I just wanted to ask a quick question regarding datasets. I am creating 3 tables using a dataadapter. what i want to know is that is the relationship created between these datatables...
2
by: Z D | last post by:
Hello, I'm currently using Remoting (HTTP/Binary) to remote a simple object. Everything is working fine except for one function that returns an arraylist of datatables. When I call this...
4
by: sal | last post by:
Greets, All Converting array formula to work with datatables/dataset tia sal I finally completed a formula I was working on, see working code below. I would like to change this code so it...
4
by: Job Lot | last post by:
Is there anyway of Joining two or more DataTable with similar structure? I have three DataTables with following structures Data, AmountB/F, Repayments, InterestCharged and AmountC/F i want...
5
by: Frank | last post by:
Hello All, I am working on a vb.net app where I need to compare to 2 datatables and determine if a string exists in one or both. The first dt is filled from the db. A form is loaded and the...
3
by: bbdobuddy | last post by:
Hi, I have two datatables that I want to left outer join and then do some queries on but I having a hard time figuring out how to join the datatables together. One of the datatables comes from...
7
by: JoNo216 | last post by:
hey guys, I'm a little new when it comes to the ASP environment, and I need a little help... Does anyone know how to combine two separate datatables, and get them to display one after the other...
0
by: StefanPienaar | last post by:
Hi Guys Is there any way in c# (or vb.net) to extract a datatable of data from a dataset with multiple datatables which has relationships set up (containing combined data from the datatables)? ...
2
by: =?Utf-8?B?QVZM?= | last post by:
Hi, Ive two data tables..I need to perform a join on these datatables.. and fetch the data..I need to do it programaticlaly.. How can I acheive it...any sample code wpuld be of great help..
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.