Do you mean count instead of sum?
Laravel Query Builder, I Need Help in Write Query
I have a problem can you people help me to getting out from this problem.
I have table of patients. here is table schema.
Schema::create('patients', function (Blueprint $table) {
$table->id();
$table->string('first_name');
$table->string('last_name');
$table->string('slug')->nullable();
$table->string('date_of_birth')->nullable();
$table->string('account_number');
$table->string('received_date');
$table->string('procedure_date');
$table->text('comment');
$table->string('auth');
$table->string('auth_status')->nullable();
$table->string('auth_reconsideration')->nullable();
$table->foreignId('provider_id')->constrained('providers')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
});
and here is provider table schema:
Schema::create('providers', function (Blueprint $table) {
$table->id();
$table->string('provider_name');
$table->string('slug')->nullable();
$table->timestamps();
});
I want to write only query that can do my task.
Here the explanation of my query detail that i want to create.
task list:
1: count of patients record by provider_id
2: count of auth (auth have default value 'Yes' or 'No') where auth="Yes" by provider_id
3: count of auth status ( auth status have three default value "Approved", "Denied" or "Pending") by provider_id
Can we write query of these three task in one query?
Thanks in advance.
Try this:
$records = Patient::query()
->select([
'provider_id',
DB::raw('COUNT(*) AS patients_count'),
DB::raw("COUNT(CASE WHEN auth = 'Yes' THEN 1 ELSE NULL END) AS auth_yes_count"),
DB::raw("COUNT(CASE WHEN auth = 'No' THEN 1 ELSE NULL END) AS auth_no_count"),
DB::raw("COUNT(CASE WHEN auth_status = 'Approved' THEN 1 ELSE NULL END) AS status_approved_count"),
DB::raw("COUNT(CASE WHEN auth_status = 'Denied' THEN 1 ELSE NULL END) AS status_denied_count"),
DB::raw("COUNT(CASE WHEN auth_status = 'Pending' THEN 1 ELSE NULL END) AS status_pending_count"),
])
->groupBy('provider_id')
->withCasts([
'patients_count' => 'integer',
'auth_yes_count' => 'integer',
'auth_no_count' => 'integer',
'status_approved_count' => 'integer',
'status_denied_count' => 'integer',
'status_pending_count' => 'integer',
])
->get();
You can skip the ->withCasts(...) part if you are not using Laravel 7. That would convert your columns to integer instead of strings.
If you are not using Eloquent Models, but the query builder directly, use this:
$records = DB::table('patients')
->select([
'provider_id',
DB::raw('COUNT(*) AS patients_count'),
DB::raw("COUNT(CASE WHEN auth = 'Yes' THEN 1 ELSE NULL END) AS auth_yes_count"),
DB::raw("COUNT(CASE WHEN auth = 'No' THEN 1 ELSE NULL END) AS auth_no_count"),
DB::raw("COUNT(CASE WHEN auth_status = 'Approved' THEN 1 ELSE NULL END) AS status_approved_count"),
DB::raw("COUNT(CASE WHEN auth_status = 'Denied' THEN 1 ELSE NULL END) AS status_denied_count"),
DB::raw("COUNT(CASE WHEN auth_status = 'Pending' THEN 1 ELSE NULL END) AS status_pending_count"),
])
->groupBy('provider_id')
->get();
Please or to participate in this conversation.