Question

I have a database from which I call data according to checkboxes checked(Retrieve data from sql database and display in tables - Display certain data according to checkboxes checked). I want to add an option of calculating their average (Between the wanted dates). How can I write a query, calculating the averages when the output is not something specific?

This is the query that calls my data at first place:

$tmp ="SELECT DISTINCT ".implode(",", $sql_columns)." FROM $database_Database_Test.$table_name where DATE>=\"$fromdate\" AND DATE<=\"$todate\"";

How can the average-query call the data according to checkboxes checked + don't average the date and hour column:

$average="SELECT AVG(",", $sql_columns) AS PriceAverage FROM $database_Database_Test.$table_name where DATE>=\"$fromdate\" AND DATE<=\"$todate\""; 

Here is my attempt but don't seem to work. The problem is that the AVG is for strings and I want to display an array.

Was it helpful?

Solution

Firstly you state a variable that matches your columns [inside the parenthesis e.g (DNI)] and give the command to calculate their average. Finally name the average e.g 'adni'.

$avg_names=array('1'=>'AVG(DNI) as adni', '2'=>'AVG(GHI) as aghi', '3'=>'AVG(Zenith_Angle) as azenith', '4'=>'AVG(DiffuseHI) as adhi');

$average="SELECT ".implode(",", $avg_names)." FROM $database_Database_Test.$table_name where DATE>=\"$fromdate\" AND DATE<=\"$todate\""; 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top