Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

Laravel Nova Metrics - Use Aggregate Function

If I am trying to display the total number of distinct customers, it would as simple as follows:

return $this->count(
    $request,
    Order::select('customer_id')->distinct(),
    'customer_id'
);

But what if I am trying to display the total number of repeating customers. Repeating customers are those who appear multiple times in orders table. What I am thinking of is as follows:

$query = Order::select(DB::raw('count(*) as count, customer_id'))
     ->groupBy('customer_id')
    ->having('count', '>', 1);
return $this->sum(
    $request,
    $query,
    'count'
);

But I keep getting the following error:

message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'count' in 'field list' (SQL: select sum(`count`) as aggregate from `orders` where `created_at` between 2020-10-31 23:56:53 and 2020-11-30 23:56:53 group by `customer_id` having `count` > 1)"

How can I fix such issue?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...