Link to home
Start Free TrialLog in
Avatar of charlieb01
charlieb01

asked on

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

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
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

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

Avatar of charlieb01
charlieb01

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Rose Babu
Rose Babu
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect, this is exactly what I needed. Tested it and it works perfectly.

Thanks,
Charlie