Solved

VB.NET - Need to get average of data in datatable

Posted on 2014-10-15
4
1,096 Views
Last Modified: 2014-10-15
Using VB.NET 2013, I have a database table named 'Measurement' that contains columns of temperature, pressure and flow measurements so I have columns named like: TE01, TE02, PE01, PE02, etc. along with a column with a date/time stamp and a column named 'PHASE' . The PHASE can be either 1, 2, or 3 and I need the AVERAGE of the data from each measurement column only for PHASE 3.

qryMeas = "SELECT * FROM MEASUREMENT WHERE PHASE = 3"
daMeas = new oledbDataAdapter(qryMeas, conStr)
dtMeas = new oledbDataTable

daMeas.Fill(dtMeas)

' At this point dtMeas contains all of the data from the MEASUREMENT database table where PHASE = 3

Now what I need to know is how to get the AVERAGE for each of the measurement columns without adding them up and dividing by the row count. I think there should be a simple way to do this I just can't seem to figure it out.

Can someone provide sample code that they know works. I have looked on the Microsoft website regarding Datatable Compute Method but that talks about datasets and objects and I tried adapting their example code but couldn't get it to work.

Help please!

Thanks,
Charlie
0
Comment
Question by:charlieb01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 40381847
Why don't you let the SQL Server do the job?
qryMeas = "SELECT AVG(TE01), AVG(TE02), AVG(PE01), AVG(PE02)
FROM MEASUREMENT 
WHERE PHASE = 3"

Open in new window

0
 

Author Comment

by:charlieb01
ID: 40381860
Vitor,
The problem with the code you suggest is that my code won't always know the names of the columns and there could be as many as 300 columns of sensor readings which would make for a very long query.
0
 
LVL 16

Accepted Solution

by:
Rose Babu earned 500 total points
ID: 40382062
try the below code
Dim avgVal As Integer = -1
Dim strFieldName As String = String.Empty

strFieldName = "TE01" ' You can change it dynamically with the valid column name
avgVal = dtTable.Compute("Avg([" & strFieldName & "])", "")

Open in new window

Try this and let us know...
0
 

Author Closing Comment

by:charlieb01
ID: 40382669
Perfect, this is exactly what I needed. Tested it and it works perfectly.

Thanks,
Charlie
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

635 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