Laravel STDDEV与LIMIT

时间:2014-04-16 20:20:44

标签: php mysql laravel-4 aggregate standard-deviation

我正在尝试使用Laravel在数据子集上获取AVG()和STDDEV_SAMP()。

所以我试过

//Data from which I want to calculate the AVG and STDDEV_SAMP()
//Limit the query to certain values
$subquery = TableAModel::join('TableB','TableA.TableB_id','=','TableB.id')
                                ->select('Factor1')
                                ->whereraw('conditionA <= 10')
                                ->orderBy('DateTimeCode', 'desc')
                                ->take('5')
                                ->toSql();

//My aggregate functions     
    $aggregates = 'AVG(TableA.Factor1) as Factor1,
                        STDDEV_SAMP(TableA.Factor1) as Factor1_SD';
//My final query that should return the average and SD                                
    $AVG_SD = \DB::table(\DB::raw(" ($subquery) as sub "))->select(\DB::raw("$aggregates"))->first()->toArray();

//it should return something like array([Factor1] => The_average, [Factor1_SD] => The_SD)

但是,它会在“字段列表”中抛出“未找到列:1054未知列'TableA.Factor1'”。如果我尝试select('*'),它会抛出“重复列id”。

1 个答案:

答案 0 :(得分:2)

我对此没有任何经验,但在我看来,就像你创建一个视图一样,从那个视图中你想要找到Factor1的STD和AVG。但是你说AVG(TableA.Factor1)为Factor1,我认为TableA不会是DB::table(\DB::raw(" ($subquery) as sub "))的表名,所以它找不到因子1,因为它找不到表。表不会被称为子。

我不知道是否是这种情况,但希望它有所帮助,也会留下评论,但我还没有代表:(。