vendredi 29 décembre 2017

Laravel raw query that counts results per month and aggregates

I have the following query

$this->builder = (DB::table('sales')
        ->select(DB::raw('DATE_FORMAT(sale_date, "%M") as sale_date'), DB::raw('count(*) as sales'))
        ->where('sale_date', '>', $lookback->toDateTimeString())
        ->orderBy('sale_date', 'asc')
        ->groupBy('sale_date'));

Despite specifying the month %M to group the results, it's just counting each date and giving the label as the month, so something like this:

sale_date , sales
 August   ,  1
 August   ,  3
 August   ,  2

Instead of what I expect:

sale_date , sales
 August   ,  6

What am I doing wrong here, I do have the option of just summing those within the javascript once I get the results from the API, however I don't think it should be necessary right?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2zMdXY6
via IFTTT

Aucun commentaire:

Enregistrer un commentaire