I am using codeigniter 4. I want to sum this data from my model.
pow(data_training.area-avg(area),2)
so I type like this :
sum(pow(data_training.area-avg(area),2))
but it’s turn out like this:
error screenshot
Please tell me how to sum the data values. Here is my full code :
public function getVariance() {
return $this->db->table('data_training')
->select('pow(data_training.area-avg(area),2)/(count(kelas)-1) as areaVariance, pow(majorAxis-avg(majorAxis),2)/(count(kelas)-1) as majorAxisVariance, pow(minorAxis-avg(minorAxis),2)/(count(kelas)-1) as minorAxisVariance, pow(eccentricity-avg(eccentricity),2)/(count(kelas)-1) as eccentricityVariance, pow(convexArea-avg(convexArea),2)/(count(kelas)-1) as convexAreaVariance, pow(extent-avg(extent),2)/(count(kelas)-1) as extentVariance, pow(perimeter-avg(perimeter),2)/(count(kelas)-1) as perimeterVariance')
->groupBy('kelas')
->get()->getResultArray();
}
and here is my query that worked. So I tried to translate it to php code but i don’t know how.
select sum(pow(dtr.area-atr.area,2))/(jml-1) as AreaVariance, sum(pow(dtr.majorAxis-atr.majorAxis,2))/(jml-1) as majorAxisVariance, sum(pow(dtr.minorAxis-atr.minorAxis,2))/(jml-1) as minorAxisVariance, sum(pow(dtr.eccentricity-atr.eccentricity,2))/(jml-1) as eccentricityVariance, sum(pow(dtr.convexArea-atr.convexArea,2))/(jml-1) as convexAreaVariance, sum(pow(dtr.extent-atr.extent,2))/(jml-1) as extentVariance, sum(pow(dtr.perimeter-atr.perimeter,2))/(jml-1) as perimeter, dtr.kelas from data_training dtr join ( select count(area) as jml,avg(area) as area, avg(majorAxis) as majorAxis, avg(minorAxis) as minorAxis, avg(eccentricity) as eccentricity, avg(convexArea) as convexArea, avg(extent) as extent, avg(perimeter) as perimeter, kelas from data_training group by kelas) atr on dtr.kelas = atr.kelas group by dtr.kelas;
2
Answers
You can use subqueries.
You can rewrite your getVariance() function:
try this one
}