require 'bigdecimal' def main(orchAttr, sqlConn, inputMap) return_hash = {} #get the vineyard string from inputs vineyard = inputMap["Vineyard"].to_s begin #define the select statement selectStmt = "SELECT AVG(MOISTURE), AVG(HIGH) FROM VINEYARD_MOISTURE WHERE LOCATION LIKE ? ORDER BY DATE_READ DESC" #create a prepared statement with the SQL preparedStmt = sqlConn.prepareStatement(selectStmt) #set the input value in the parepared statement preparedStmt.setString(1, vineyard +"%") #execute the query returning the result set resultSet = preparedStmt.executeQuery() #use the helper to set the records from the result set to a data set variable #this will put the results in an array in the return_hash with fields for each requested column average moisture, average high return_hash = orchAttr.mapResultSetToDataSet(resultSet, "averages") #return the resulting values #just get the two requested values and put them in individual outputs (rounding to two decimals), an array is not needed return_hash["averageMoisture"] = return_hash["averages"][0]["AVG(MOISTURE)"].to_f.round(2).to_s return_hash["averageHigh"] = return_hash["averages"][0]["AVG(HIGH)"].to_f.round(2).to_s orchAttr.writeWarn("first: " + return_hash["averages"][0].to_s) orchAttr.writeWarn("first avg moisture: " + return_hash["averages"][0]["AVG(MOISTURE)"].to_s) orchAttr.writeWarn("first avg high: " + return_hash["averages"][0]["AVG(HIGH)"].to_s) #close the result and statement resultSet.close() preparedStmt.close() rescue Exception => e averageError = e.to_s orchAttr.writeWarn(e.to_s); end if averageError != nil return_hash["averageError"] = averageError else return_hash["averageError"] = "false" end orchAttr.writeWarn("return_hash " + return_hash.to_s) return return_hash end