27

i'm trying to group my data by month and year.

$data ->select(DB::raw('count(id) as `data`'),DB::raw('YEAR(created_at) year, MONTH(created_at) month'))
           ->groupby('year','month')
           ->get();

The output is :

{
"data": 19215,
"year": 2016,
"month": 10
},

if i group only by month, i don't know from which year belong this month, my expected output is :

{
"clicks": 19215,
"month": 11-2016,
},
{
"clicks": 11215,
"month": 12-2016,
},

i want to do it in sql, not in php.

Adrian Edy Pratama
  • 3,841
  • 2
  • 9
  • 24
TheShun
  • 1,128
  • 6
  • 15
  • 21

7 Answers7

55

You can try as:

->select(DB::raw('count(id) as `data`'), DB::raw("DATE_FORMAT(created_at, '%m-%Y') new_date"),  DB::raw('YEAR(created_at) year, MONTH(created_at) month'))
->groupby('year','month')
->get();
Amit Gupta
  • 17,072
  • 4
  • 41
  • 53
28

Note: This is solution based on eloquent, and yes, it not the best approach, but as the question is, this is using eloquent. If you want faster way, you can do it with raw query.

If you want to group your results for example by year, you can use closure where you will parse your datetime to desired format (year or month).

Here is example of the code:

$res= ModelName::where('someColumn','test')
      ->get()
      ->groupBy(function($val) {
      return Carbon::parse($val->created_at)->format('Y');
});
Brane
  • 3,257
  • 2
  • 42
  • 53
19
$result = ModelName::selectRaw('year(created_at) year, monthname(created_at) month, count(*) data')
                ->groupBy('year', 'month')
                ->orderBy('year', 'desc')
                ->get();
otim fredrick
  • 351
  • 3
  • 5
  • Thanks! In my case I wanted to order by month too. So my `select` became `monthname(created_at) month_name, month(created_at) month` and then `->groupBy('year', 'month', 'month_name')->orderBy('year', 'desc')->orderBy('month', 'desc')`. – JCarlosR Mar 06 '23 at 04:18
7

for latest version of mysql, the accepted answer will give error, so instead try

$data
    ->selectRaw("
        count(id) AS data, 
        DATE_FORMAT(created_at, '%Y-%m') AS new_date, 
        YEAR(created_at) AS year, 
        MONTH(created_at) AS month
    ")
    ->groupBy('new_date')
    ->get();

ref: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format

ctf0
  • 6,991
  • 5
  • 37
  • 46
2
$data ->select(DB::raw('count(id) as `data`'),DB::raw('YEAR(created_at) year'),DB::raw('MONTH(created_at) month'))
       ->groupBy(DB::raw('YEAR(created_at)'), DB::raw('MONTH(created_at)'))
       ->get();
umar Aslam
  • 39
  • 9
2

The answer by @Amit Gupta is a good, but it isn't working >= MySQL 5.7/MariaDB 10.2 and calls the error:

Syntax error or access violation: 1055 Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column

To solve that issue, you can use eg. MIN() on the nonaggregated column:

->select(
    DB::raw("
    count(id) as data, 
    MIN(DATE_FORMAT(created_at, '%m-%Y')) as new_date, 
    YEAR(created_at) year, 
    MONTH(created_at) month
    ")
)
->groupBy('year', 'month')
->get();
wittich
  • 2,079
  • 2
  • 27
  • 50
1

=>this might help someone in 2019

$date = "2019-06-16";

return date('m', strtotime($date)); //this return you 06(edited)

so you can do foreach by this method , so you don't need to do the all sql things..

ppegu
  • 362
  • 2
  • 9
  • 22