Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Recursive function problem

Posted on 2006-11-14
Medium Priority
223 Views
I have a datatable with the following columns:

I need to sum the number columns for each Heading and Subheading. How do I do this using a recursive function? I vaguely remember recursion when I was studying, is it the best approach? The select statement that fills this datatable is ordered by Heading, Subheading.

In the future I may need to add deeper levels below the sub heading. I just need some pointers on how to get started writing the algorithm, the general idea of how to solve this.

Cheers

0
Question by:craigdev
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 43

Expert Comment

ID: 17936789
Hi craigdev,

Generally it is easier to let SQL do this for you if you already have the data there:

Tim Cottee
0

LVL 1

Author Comment

ID: 17936837
true, thanks but then I have to write multiple select statements and try and combine all the results into a big table. What Im trying to do is make a versatile function that I can use to sum up any columns using a single select. The table that I mentioned may actually have several Heading and subheading columns and number columns. If I can get it to work in the case above then I can tweak the algorithm a bit for any case.
0

LVL 43

Accepted Solution

TimCottee earned 2000 total points
ID: 17937069
craigdev,

Ok, that makes it a slightly more interesting problem.

The following function returns a datatable when supplied with the source datatable, number of groupings and number of columns to sum.

The requirements are that the datatable has the grouping columns first, followed by the columns to total and that the ordering of the source table is heading,subheading,subsubheading etc.

Private Function SumTable(ByRef Source As DataTable, ByVal Groups As Int16, ByVal Sums As Int16) As DataTable
Dim Result As DataTable = New DataTable
Dim aryGroups() As String
ReDim aryGroups(Groups)
Dim intGroup As Int16
For intGroup = 0 To Groups - 1
Next
For intGroup = 0 To Sums - 1
Next
Dim intRow As Integer
Dim TargetRow As DataRow
For intRow = 0 To Source.Rows.Count - 1
Dim SourceRow As DataRow = Source.Rows(intRow)
Dim NewRow As Boolean = False
For intGroup = 0 To Groups - 1
If CType(SourceRow.Item(intGroup), String) <> aryGroups(intGroup) Then
NewRow = True
End If
Next
If NewRow Then
TargetRow = Result.NewRow
For intGroup = 0 To Groups - 1
aryGroups(intGroup) = CType(SourceRow.Item(intGroup), String)
TargetRow.Item(intGroup) = CType(SourceRow.Item(intGroup), String)
Next
For intGroup = 0 To Sums - 1
TargetRow.Item(Groups + intGroup) = 0
Next
End If
For intGroup = 0 To Sums - 1
Result.Rows(Result.Rows.Count - 1).Item(Groups + intGroup) = CType(CType(Result.Rows(Result.Rows.Count - 1).Item(Groups + intGroup), Double) + CType(Source.Rows(intRow).Item(Groups + intGroup), Double), String)
Next
Next
Return Result
End Function

This then will take any datatable in the appropriate format and return it summed on the various headings.

Tim
0

LVL 1

Author Comment

ID: 17937160
thanks heaps that worked perfectly!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
###### Suggested Courses
Course of the Month6 days, 9 hours left to enroll