尊敬的专家,我想在以下方面寻求您的帮助
我想在同一查询中的仪表板中按类别发送名称,数量和比率(此状态的数量占同一类别帖子总数的比率)。
使用以下命令,我可以返回名称,计数,但是我也不知道如何计算比率。多谢您的帮助
//Top claim solved by category
$solvedByCategoryCollection = Post::join('categories', 'categories.id', '=', 'posts.category_id')
->groupBy('categories.id')
->where('posts.status_id', '3')
->get(['categories.id', 'categories.name', DB::raw('count(categories.id) as count')]);
答案 0 :(得分:1)
这会做这项工作吗?
//Top claim solved by category
$solvedByCategoryCollection = Post::join('categories', 'categories.id', '=', 'posts.category_id')
->groupBy('categories.id')
->where('posts.status_id', '3')
->get([
'categories.id',
'categories.name',
DB::raw('count(categories.id) as count'),
DB::raw(sprintf('(count(*)/(%s) ) as ratio', Post::select(DB::raw('COUNT(*)'))->toSql()))
]);