473,386 Members | 1,752 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,386 software developers and data experts.

Aggregate functions with GROUP BY Clauses on DataTable?

Is it possible to use Aggregate functions with GROUP BY Clauses on DataTable. I have a DataTable with following values:

Date Amount Int Balance
1/1/2004 5000.00 50.00 5050.00
1/1/2004 4000.00 40.00 4040.00
1/2/2004 1000.00 10.00 1010.00
1/2/2204 2000.00 20.00 2020.00
1/3/2004 1500.00 15.00 1515.00

I want my resultant DataTable to show in DataGrid as

Date Amount Int Balance
1/1/2004 9000.00 90.00 9090.00
1/2/2004 3000.00 30.00 3030.00
1/3/2004 1500.00 15.00 1515.00

Thanx
Nov 22 '05 #1
1 8059
Job Lot,
I find the easist way is to manually create an Aggregate table.

I created the following a while ago.

Which you might be able to modify for your needs...

Option Strict On
Option Explicit On

Public Class Grouping

Private ReadOnly ds As DataSet
Private ReadOnly g1, g2, g3, d As DataTable

Public Sub New()
Dim c1, c2, c3, c4 As DataColumn
ds = New DataSet("Grouping")

' define the group 1 table
g1 = ds.Tables.Add("g1")
c1 = g1.Columns.Add("c1", GetType(Integer))
g1.PrimaryKey = New DataColumn() {c1}

' define the group 2 table
g2 = ds.Tables.Add("g2")
c1 = g2.Columns.Add("c1", GetType(Integer))
c2 = g2.Columns.Add("c2", GetType(Integer))
g2.PrimaryKey = New DataColumn() {c1, c2}

' define the group 3 table
g3 = ds.Tables.Add("g3")
c1 = g3.Columns.Add("c1", GetType(Integer))
c2 = g3.Columns.Add("c2", GetType(Integer))
c3 = g3.Columns.Add("c3", GetType(Integer))
g3.PrimaryKey = New DataColumn() {c1, c2, c3}

' define the data table
d = ds.Tables.Add("d")
c1 = d.Columns.Add("c1", GetType(Integer))
c2 = d.Columns.Add("c2", GetType(Integer))
c3 = d.Columns.Add("c3", GetType(Integer))
c4 = d.Columns.Add("c4", GetType(Integer))

End Sub

Public Sub Populate(ByVal count As Integer, ByVal maxValue As Integer)
Dim rand As New Random
Dim c1, c2, c3, c4 As Integer
For index As Integer = 1 To count
c1 = rand.Next(1, maxValue)
c2 = rand.Next(1, maxValue)
c3 = rand.Next(1, maxValue)
c4 = rand.Next(1, maxValue)
d.Rows.Add(New Object() {c1, c2, c3, c4})
Next
End Sub

Public Sub GroupBy()
For Each row As DataRow In d.Rows
AddGroup(g1, row!c1)
AddGroup(g2, row!c1, row!c2)
AddGroup(g3, row!c1, row!c2, row!c3)
Next
ds.Relations.Add(New DataColumn() {g1.Columns("c1")}, New
DataColumn() {d.Columns("c1")})
g1.Columns.Add("t1", GetType(Integer), "sum(child.c4)")
g1.Columns.Add("t2", GetType(Integer), "avg(child.c4)")
g1.Columns.Add("t3", GetType(Integer), "min(child.c4)")
g1.Columns.Add("t4", GetType(Integer), "max(child.c4)")
g1.Columns.Add("t5", GetType(Integer), "count(child.c4)")
g1.Columns.Add("t6", GetType(Integer), "stdev(child.c4)")
g1.Columns.Add("t7", GetType(Integer), "var(child.c4)")
End Sub

Private Sub AddGroup(ByVal group As DataTable, ByVal ParamArray keys()
As Object)
If group.Rows.Find(keys) Is Nothing Then
group.Rows.Add(keys)
End If
End Sub

Public Sub Save(ByVal fileName As String)
ds.WriteXml(fileName)
End Sub

Public Sub Print()
For Each row As DataRow In g1.Rows
Debug.WriteLine(row!c1.ToString())
Debug.Indent()
Debug.WriteLine(row!t1, "sum")
Debug.WriteLine(row!t2, "avg")
Debug.WriteLine(row!t3, "min")
Debug.WriteLine(row!t4, "max")
Debug.WriteLine(row!t5, "count")
Debug.WriteLine(row!t6, "stdev")
Debug.WriteLine(row!t7, "var")
Debug.Unindent()
Next
End Sub

Public Shared Sub Main()
Dim luke As New Grouping
luke.Populate(10000, 16)
luke.GroupBy()
luke.Save("Grouping.xml")
luke.Print()
End Sub

End Class

By defining the relationships between g1, g2, g3 & the d table, you can use
GetChildRows to get the rows associated with a specific group. Or use the
"child" expression syntax to get aggregate amounts... I show child
aggregates...

Hope this helps
Jay
"Job Lot" <Jo****@discussions.microsoft.com> wrote in message
news:EE**********************************@microsof t.com...
Is it possible to use Aggregate functions with GROUP BY Clauses on DataTable. I have a DataTable with following values:
Date Amount Int Balance
1/1/2004 5000.00 50.00 5050.00
1/1/2004 4000.00 40.00 4040.00
1/2/2004 1000.00 10.00 1010.00
1/2/2204 2000.00 20.00 2020.00
1/3/2004 1500.00 15.00 1515.00

I want my resultant DataTable to show in DataGrid as

Date Amount Int Balance
1/1/2004 9000.00 90.00 9090.00
1/2/2004 3000.00 30.00 3030.00
1/3/2004 1500.00 15.00 1515.00

Thanx

Nov 22 '05 #2

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

Similar topics

6
by: Steven An | last post by:
Howdy, I need to write an update query with multiple aggregate functions. Here is an example: UPDATE t SET t.a = ( select avg(f.q) from dbo.foo f where f.p = t.y ), t.b = ( select sum(f.q)...
1
by: Job Lot | last post by:
Is it possible to use Aggregate functions with GROUP BY Clauses on DataTable. I have a DataTable with following values: Date Amount Int Balance 1/1/2004 5000.00 50.00 5050.00...
3
by: eddiec | last post by:
hi everyone, I have a report in an adp that uses aggregate functions in its record source and I am trying to figure out how to filter the records displayed in the report: DoCmd.OpenReport...
10
by: neb | last post by:
Dear member of the forum, Ms access has built-in aggregate function like: -Sum, Max, First, Avg, ... Is it possible to build user-defined aggregate? (if you have any clue, do not hesitate to...
3
by: MrNobody | last post by:
I've read that the expression property for DataColumns is used to "Sets or retrieves the expresssion used to filter rows, calculate the values in a column, or create an aggregate column.". I...
2
by: Marcel Hug | last post by:
Hi NG! With a Inner-Join SQL I get my datas in a DataSet. In the table are the column Entry and Version. Like this: Entry Version 1 1 1 2 1 ...
3
by: S P Arif Sahari Wibowo | last post by:
Hi! I would like to make an editable continous form, where most fields will be from table A and editable, except 1-3 fields are a glimpse into table B and uneditable. Table A relate to table B...
0
by: BillCo | last post by:
just wasted a long time figuring out this and I figure if I post it might save someone some pain! Jet (DAO) will allow you to to use nested aggregate functions like building blocks, e.g.: ...
5
by: BillCo | last post by:
I just wasted a long time figuring out this and I figure if I post it might save someone some pain! Jet (DAO) will allow you to to use nested aggregate functions like building blocks, e.g.: ...
6
by: troy_lee | last post by:
I am trying to count the total number of units for a given part number that have a Priority rating of 1. Based upon some research, this is what I came up with for my query. Access says that I have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.