Solved

Datatable grouping

Posted on 2014-12-30
5
175 Views
Last Modified: 2014-12-30
Hi,

I have data table and example of the data could be this

Key    Data
1        5
2        2
1        1
3        4

I need some way of grouping and summing the data so that what I end with is this:

Key    Data
1        6
2        2
3        4

Basically I need the key column to unique and the data column to be the sum of all the common key values

Is this even possible?
0
Comment
Question by:andyw27
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:Ali HND
ID: 40523929
SELECT key,  SUM (data)  FROM YourTableName  GROUP BY key;

Open in new window

0
 

Author Comment

by:andyw27
ID: 40523941
Sorry, its a data table, not a SQL table
0
 
LVL 3

Accepted Solution

by:
Ali HND earned 500 total points
ID: 40523970
If your columns was:
Id(key),Value(data)
Imports System.Data
Imports System.Linq

Class Program
	Private Shared Sub Main(args As String())
		Dim table As New DataTable()

		Dim query = From grp In From row In table.AsEnumerable()Group row By row.Field(Of Integer)("Id")Order By grp.KeyNew With { _
			Key .Id = grp.Key, _
			Key .Sum = grp.Sum(Function(r) r.Field(Of Decimal)("Value")) _
		}

		For Each grp As var In query
			Console.WriteLine("{0}" & vbTab & "{1}", grp.Id, grp.Sum)
		Next
	End Sub
End Class

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40523973
I reckon you need to go thru the table and sum up the values yourself.
0
 

Author Closing Comment

by:andyw27
ID: 40524369
Many Thanks, a particularly useful answer.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question