DB::table('reports')
->select(DB::raw('
FLOOR(count(1)) as clicks,
ROUND(count(1) * avg(quality),0) as clicks_calitate,
ROUND(avg(quality) * 100,2) as quality,
ROUND(sum(price),3) as earnings,
ROUND((sum(price) / (count(1) * avg(quality))),3) as ecpc,
ROUND((sum(price) * 1000 / (count(1) * avg(quality))),3) as ecpm,
created_at,
DATE_FORMAT(created_at, "%m-%d-%Y") as date
'))
->where('user_id', '=', access()->user()->id)
->whereDate('created_at','>=',$start_date)
->whereDate('created_at','<=',$end_date)
->groupBy('date')
->get();
asta e unul dintre cele mai solicitante quere-uri.
Si mai este unul:
DB::table('reports')
->select(DB::raw('
FLOOR(count(1)) as clicks,
ROUND(count(1) * avg(quality), 0) as clicks_calitate,
ROUND(avg(quality) * 100, 2) as quality,
ROUND(sum(price),3) as earnings,
ROUND(sum(price) / (count(1) * avg(quality)), 3) as ecpc,
ROUND(sum(price) * 1000 / (count(1) * avg(quality)), 3) as ecpm,
created_at,
DATE_FORMAT(created_at, "%m-%d-%Y") as date
'))
->where('user_id', '=', access()->user()->id)
->whereDate('created_at','>=',$start_date)
->whereDate('created_at','<=',$end_date)
->groupBy('user_id')
->get();
Si asta, care e apelat printr-o procedura stocata:
SELECT * from (
(SELECT if(sum(price),sum(price),0) as today FROM `reports` FORCE INDEX (created_at) WHERE user_id=_user_id AND DATE_FORMAT(created_at,'%m-%d-%Y') = DATE_FORMAT(NOW(),'%m-%d-%Y')) as today,
(SELECT if(sum(price),sum(price),0) as yesterday FROM `reports` FORCE INDEX (created_at) WHERE user_id=_user_id AND DATE_FORMAT(created_at,'%m-%d-%Y') = DATE_FORMAT(subdate(NOW(), 1),'%m-%d-%Y')) as yesterday,
(SELECT if(sum(price),sum(price),0) as month_my FROM `reports` FORCE INDEX (created_at) WHERE user_id=_user_id AND DATE(created_at) > DATE(subdate(NOW(), 30))) as thisMonth )
Astea 3 dureaza cel mai mult. In rest e super ok.