Я пишу запрос для выполнения агрегированного результата, который будет суммировать каждый столбец на основе указанных условий:
Контроллер
$revenues = DB::table('vw_monthly_revenue_report')
->select(
"channel"
,DB::raw("(SELECT SUM(amount) FROM vw_monthly_revenue_report WHERE billing_type = 'subscription' AND channel = '9mobile') as total_9mobile_subscription")
,DB::raw("(SELECT SUM(amount) FROM vw_monthly_revenue_report WHERE (billing_type != 'subscription' AND channel = '9mobile') OR (billing_type != 'subscription' AND channel = '9mobile USSD')) as total_9mobile_onetime")
,DB::raw("(SELECT SUM(amount) FROM vw_monthly_revenue_report WHERE billing_type = 'subscription' AND channel = 'Airtel') as total_airtel_subscription")
,DB::raw("(SELECT SUM(amount) FROM vw_monthly_revenue_report WHERE (billing_type != 'subscription' AND channel = 'Airtel') OR (billing_type != 'subscription' AND channel = 'Airtel USSD')) as total_airtel_onetime")
,DB::raw("(SELECT SUM(amount) FROM vw_monthly_revenue_report WHERE billing_type = 'subscription' AND channel = 'MTN') as total_mtn_subscription")
,DB::raw("(SELECT SUM(amount) FROM vw_monthly_revenue_report WHERE (billing_type != 'subscription' AND channel = 'MTN') OR (billing_type != 'subscription' AND channel = 'MTN USSD')) as total_mtn_onetime")
,DB::raw("(SELECT SUM(amount) FROM vw_monthly_revenue_report WHERE billing_type != 'subscription' AND channel = 'GTB-737') as total_gtb_onetime")
,DB::raw("(SELECT SUM(amount) FROM vw_monthly_revenue_report WHERE billing_type != 'subscription' AND channel = 'UBA-919') as total_uba_onetime")
,DB::raw("(SELECT SUM(amount) FROM vw_monthly_revenue_report WHERE billing_type != 'subscription' AND channel = 'Unity') as total_unity_onetime")
,DB::raw("SUM(amount) as total_revenue")
,DB::raw('DATE(created_at) as created_at'))
->groupBy(DB::raw('DATE(created_at)'))
->orderByRaw('created_at DESC');
Я обнаружил, что получаю одинаковый результат для всех столбцовкроме общего итога.
Я получил результат, как показано ниже:
Где я пропустил это и как мнеперепишите запрос.
Спасибо