views:

376

answers:

1

I have a datatable object, which is populated from a webservice.

Apparently, the web service just throws everything (data) back to me. The data which gets in my datatable looks like this:

Dept      Code    Value
Science   ABC     5
Science   ABC     6
Science   DEF     7
Math      ABC     3
Math      DEF     9
English   ABC     2
English   DEF     3
English   DEF     4
English   DEF     5

Now, I want to create a datatable that will calculate (and sum)/ eliminate the values in the datatable, so that the new datatable would have the data like:

Dept      Code    Value
Science   ABC     11
Science   DEF     7
Math      ABC     3
Math      DEF     9
English   ABC     2
English   DEF     12

Please take note that I could only modify the datatable.

Can anyone help me? VB.Net please. Thanks.

+1  A: 

A simple summary query will give you what you want:

select
   Dept,
   Code,
   sum(Value) sum_value
from
   datatable
group by
   Dept,
   Code

You could also create a view with that SQL definition, so you could just query the view as you would a table. If you start to get so much data that the query is slow, you'll want to store the results in a permanent table - but for moderate amounts of data this should work fine.

Ron

Ron Savage
I cannot modify the SQL for this matter. As I have mentioned, I am getting a datatable object whose data is thrown in by a webservice. That is why I need to make the modifications on the datatable.
Batuta
Ahh, the actual DataTable object ... I'll poke around some more. :-)
Ron Savage