Laravel - Совокупная сумма дает неверный результат - PullRequest
0 голосов
/ 10 июля 2019

Я пишу запрос для выполнения агрегированного результата, который будет суммировать каждый столбец на основе указанных условий:

Контроллер

    $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');

Я обнаружил, что получаю одинаковый результат для всех столбцовкроме общего итога.

Я получил результат, как показано ниже:

aggregate_error

Где я пропустил это и как мнеперепишите запрос.

Спасибо

1 Ответ

0 голосов
/ 11 июля 2019

Используйте IN для проверки соответствия канала вместо ИЛИ.

$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 IN ['9mobile','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 IN ['Airtel','Airtel USSD']) OR) 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 IN ['MTN','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');
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...