Solved

Datatable grouping

Posted on 2014-12-30
5
173 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

838 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