How to sum based on a condition

Published 2 years ago by Khudadad

Hi, all. I want to sum a field in the database(via query builder) based on a condition (ie. date<6/11/2016) and don't know which way is the best. I'm doing like this, is there any other better way to do this.

    $income = DB::table('incomes')
            ->join('currencies', 'currencies.cur_id','=','incomes.currency_id')
            ->selectRaw('incomes.*, currencies.currency ,sum(incomes.total) as total')
            ->where('inc_date','>',$sd)->where('inc_date','<',$ed)->get();
        return view('report.report', compact('income','sd','ed'))->render();

thanks.

Please sign in or create an account to participate in this conversation.