Laravel-总和给出错误的结果

时间:2019-07-10 12:46:43

标签: laravel eloquent

我正在编写一个查询以执行聚合结果,该结果将根据指定条件对每一列求和:

控制器

    $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 :(得分:0)

使用IN而不是OR来检查通道是否匹配。

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