Solved

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

Posted on 2014-10-15
4
820 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
  • 2
4 Comments
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Perfect, this is exactly what I needed. Tested it and it works perfectly.

Thanks,
Charlie
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now