I need to show the call and chat duration time, use CASE WHEN to try to filter the data but if I don't add them to a groupBY it repeats the session_id and if I don't add them to the groupby it shows me an error
code that repeats the sesion_id
$query = DB::table($sub)
->select('session_id as id_sesion','categories.name As nombre_categoria','type',
DB::raw('TIME_FORMAT(SEC_TO_TIME(tiempo),"%H:%i:%s") as duracion'),
DB::raw('TRUNCATE( SUM( (points/'.$valor_poinst_value->puntos_por_dolar.') ),2) as total_sales'))
->join('consultancy_requests','session_id','=','consultancy_requests.consultancy_id')
->join('advisors','consultancy_requests.advisor_id','=','advisors.id')
->join('advisor_assignments','advisors.id','=','advisor_assignments.advisor_id')
->join('category_region','advisor_assignments.category_region_id','=','category_region.id')
->join('categories','category_region.category_id','categories.id')
->where('advisors.parent_id','!=',NULL)
->where('categories.parent_id','!=',NULL)
->where('categories.category_type_id','=',2)
->where('categories.status',1)
->where('category_region.region_id',$region_id)
->groupBy('id_sesion','categories.name','type','duracion');
$t_categorias = DB::table($query)
->select('id_sesion','nombre_categoria',DB::raw('ROUND( SUM(CASE WHEN type = "chat" then total_sales END),2) as total_chat'),
DB::raw('ROUND( SUM(CASE WHEN type = "call" then total_sales END),2) as total_call'),
DB::raw('(CASE WHEN type = "chat" THEN duracion END) AS chat_time'),
DB::raw('(CASE WHEN type = "call" THEN duracion END) AS call_time'),
DB::raw('ROUND(SUM(total_sales),2) AS totales'))
->groupBy('id_sesion','nombre_categoria','chat_time','call_time')
->get();
result that repeats the id_session
{
"id_sesion": "-N3LnZMWz2ac8O87bH8O",
"nombre_categoria": "Asuntos Familiares",
"total_chat": 0.05,
"total_call": null,
"chat_time": "00:00:07",
"call_time": null,
"totales": 0.05
},
{
"id_sesion": "-N3LnZMWz2ac8O87bH8O",
"nombre_categoria": "Asuntos Familiares",
"total_chat": null,
"total_call": 0.1,
"chat_time": null,
"call_time": "00:00:08",
"totales": 0.1
},
{
"id_sesion": "-N3LnZMWz2ac8O87bH8O",
"nombre_categoria": "Asuntos Familiares",
"total_chat": 0.05,
"total_call": null,
"chat_time": "00:00:05",
"call_time": null,
"totales": 0.05
}
result that I hope to obtain
{
"id_sesion": "NB1824y4BHu112",
"nombre_categoria": "Asuntos Familiares",
"total_chat": 0.10,
"total_call": 0.10,
"chat_time": "00:00:07",
"call_time": "00:01:10,
"totales": 0.20
},
{
"id_sesion": "-N3LnZMWz2ac8O87bH8O",
"nombre_categoria": "Asuntos Familiares",
"total_chat": 0.20,
"total_call": 0.10,
"chat_time": "00:00:50,
"call_time": "00:02:08",
"totales": 0.30
},
{
"id_sesion": "-N3ns128",
"nombre_categoria": "Asuntos Familiares",
"total_chat": 0.15,
"total_call": 0.15,
"chat_time": "00:00:05",
"call_time": "00:00:50",
"totales": 0.30
}...