473,767 Members | 2,247 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8089
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("Groupi ng")

' 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.Ad d(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(fil eName)
End Sub

Public Sub Print()
For Each row As DataRow In g1.Rows
Debug.WriteLine (row!c1.ToStrin g())
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(1 0000, 16)
luke.GroupBy()
luke.Save("Grou ping.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****@discuss ions.microsoft. com> wrote in message
news:EE******** *************** ***********@mic rosoft.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
9999
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) from dbo.foo f where f.p = t.y ) FROM dbo.test t
1
1387
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 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
3
2736
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 with a SQL string for the where condition does not work. The error returned is: The column prefix dbo.mytable does not match with a table name or alias used in the query
10
11936
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 post) *I'm running a total query, of the form
3
14315
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 have seen examples on how to filter a column, but how would I filter out an entire row depending on the value of a column? For example, if I wanted to filter out every row in a multi-column table where the "Is Fubar" column equals "true", how would...
2
40061
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
3
4594
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 in one-to-many relation. I don't need to see all values in B that relate to the particular record in A, just one value in each field in B, preferably the last entered. This is to ease a person that need to manually fix and encode
0
1593
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.: SELECT A, sum(B) as Answer1, Answer1 * 2 as DoubleAnswer
5
6098
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.: SELECT A, sum(B) as Answer1, Answer1 * 2 as DoubleAnswer
6
1891
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 invalid bracketing in the Select Distinct statement. The only brackets I inserted were to delimit the table's field names. Access did the rest. Also, Access created the second AS bracket group - . Am I barking up the right tree or is my query...
0
9571
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10168
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10009
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9838
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8835
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.