I need to group data from two queries to the same table with two different conditions, for them I use UNIONALL() but it shows me an error.
my code
$query = DB::table($sub)
->select('categories.id as categoryID','categories.name As nombre_categoria','type'
,DB::raw('TRUNCATE( SUM( (points/'.$valor_poinst_value->puntos_por_dolar.') - ((points/'.$valor_poinst_value->puntos_por_dolar.')*'.$comision.') ),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)
->groupBy('categories.id','categories.name','type','points');
$f_chat = DB::table($query)
->select('categoryID','nombre_categoria',DB::raw('SUM(total_sales) as total_chat'))
->where('type','chat')
->groupBy('categoryID','nombre_categoria');
$f_call = DB::table($query)
->select('categoryID','nombre_categoria',DB::raw('SUM(total_sales) as total_call'))
->where('type','call')
->groupBy('categoryID','nombre_categoria');
$algo = $f_call->union($f_chat);
$so = DB::table($algo)
->select('categoryID','nombre_categoria','total_call','total_chat')
->groupBy('categoryID','nombre_categoria')
->get();
error
Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'total_chat'
in 'field list' (SQL: select `categoryID`, `nombre_categoria`, `total_call`, `total_chat`
from ((select `categoryID`, `nombre_categoria`, SUM(total_sales) as total_call from (select `categories`.`id` as
`categoryID`, `categories`.`name` as `nombre_categoria`, `type`,
TRUNCATE( SUM( (points/20) - ((points/20)*0.1) ),2) as total_sales
from ((select `session_id`, `spent_points` as `points`, `type` from
the result i hope to get
