Skip to content

Instantly share code, notes, and snippets.

@codenamegary
Last active August 29, 2015 14:18
Show Gist options
  • Save codenamegary/547f4364201771ce2227 to your computer and use it in GitHub Desktop.
Save codenamegary/547f4364201771ce2227 to your computer and use it in GitHub Desktop.
Stats Refactoring
SELECT
apps.id as `id`,
apps.name as `name`,
sum(stats_today.views) as `viewsToday`,
sum(stats_total.views) as `viewsTotal`,
sum(stats_total.loads) as `loadsTotal`
FROM
apps
LEFT JOIN widgets
ON widgets.app_id = apps.id
LEFT JOIN stats as stats_today
ON stats.widget_id = widgets.id
AND stats.date = date(now())
LEFT JOIN stats as stats_total
ON stats.widget_id = widgets.id
GROUP BY
apps.id
LIMIT perPage
OFFSET ( ( page - 1 ) * perPage )
private function getAppInformation()
{
$sql = file_get_contents('stats-query.sql');
$data = DB::select($sql);
$apps = App::hydrate($data);
return $apps;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment