0

Im going to count my pallet_condition 0,1,2,3 and group it by month but i always got an error like this please help whats wrong with my code. something wrong with my date_format?

My Error

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in group statement is ambiguous (SQL: select count(*) as aggregate from `liip_psrm` inner join `liip_psrm_items` on `liip_psrm`.`id` = `liip_psrm_items`.`psrm_items_id` where date(`liip_psrm_items`.`created_at`) >= 2018-10-09 and date(`liip_psrm_items`.`created_at`) <= 2019-01-31 and `liip_psrm`.`status` = 0 and `liip_psrm`.`customer_id` = 14 and `pallet_condition` = 0 group by WEEK(created_at) order by WEEK(created_at) asc) 

My Callback function

$problem_condition_computations = function($condition) use ($psrm_maintenance, $start, $end, $customers){
        return DB::table('liip_psrm')
        ->join('liip_psrm_items', 'liip_psrm.id', '=', 'liip_psrm_items.psrm_items_id')
        ->whereDate('liip_psrm_items.created_at', '>=', $start)
        ->whereDate('liip_psrm_items.created_at', '<=', $end)
        ->select(
                DB::raw("DATE_FORMAT(liip_psrm_items.created_at, '%Y-%m-%d') AS dates
                "))
                ->where('liip_psrm.status','=', 0)
                ->where('liip_psrm.customer_id','=',$customers)
                ->where('pallet_condition', $condition)
                ->groupBy('dates')   
                ->count();
      };
BlackLeaf
  • 23
  • 1
  • 7

1 Answers1

0

"dates" field you are using in groupBy is alias name, So it do not identifying it and not working as you are expecting.

Same as you used in Select

DB::raw("DATE_FORMAT(liip_psrm_items.created_at, '%Y-%m-%d') AS dates")

Put it in GroupBy & It will work.

Hope, It will help, Let me know if you still find difficulties.

Vishal Tarkar
  • 808
  • 11
  • 32