Can somebody give me an advice how to group results in Laravel 4 by an interval of time?
So the idea is that i need to make some statistics, and i need to select the interval - 10 minutes / 30 minutes / 1 hour / 6 hours / 1 day.
My current code doesn't give me the correct results:
$stats = DB::table('orders as o')
->select(
DB::raw("DATE_FORMAT(o.created_at,'%Y-%m-%d %l:%i') as date"),
DB::raw('COUNT(o.id) as num'),
DB::raw('SUM(op.amount * op.quantity) as total'),
DB::raw('TRUNCATE(AVG(op.amount * op.quantity),0) as avg')
)
->leftJoin('order_products as op', 'o.id','=','op.order_id')
->groupBy(DB::raw('UNIX_TIMESTAMP(o.created_at) DIV ' . (Input::get('intval',10) * 60) . ''))
->where('o.status','=','verified')
->orderBy('o.created_at','ASC')
->get();
Thanks in advance!