Issue
I need to count revisited customer in one month. It means need to count customer_id has more than one entry in table within a month.
My query is showing only the total customer.
$count_customer_current_month = DB::table("customer_entry")->whereRaw('MONTH(date) = ?',
[$currentMonth])->count();
Solution
Just use group by customer_id having COUNT(customer_id) > 1
$entry_customers = DB::table("customer_entry")
->whereRaw('MONTH(date) = ?', [$currentMonth])
->groupBy('customer_id')
->havingRaw("COUNT(customer_id) > 1")
->selectRaw('COUNT(customer_id) AS entry_count, customer_id');
If you want to get how many this customers:
$entry_customers->get()->count() // count the collections.
Or use subquery to get the customers count:
DB::table(DB::raw("({$entry_customers->getSql()}) AS entry_customer"))
->mergeBindings($entry_customers)
->count();
Answered By - TsaiKoga
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.