db->table('t_gasstockdetails') ->select( 't_gasstockdetails.*, drier_summary.drierMachineGasconsumption AS drierMachineGasConsumption, drier_summary.driedSand AS sandDried, coating_summary.coatingMachineGasconsumption, coating_summary.coatedSand' ) // Subquery with early date filtering ->join( '(SELECT date, SUM(totalGasconsumption) AS coatingMachineGasconsumption, SUM(totalCoatingSandInKg) AS coatedSand FROM t_coatingmachinedetails WHERE is_active = 1 AND date >= ' . $this->db->escape($startDate) . ' AND date <= ' . $this->db->escape($endDate) . ' GROUP BY date ) AS coating_summary', 'coating_summary.date = t_gasstockdetails.date', 'left' ) ->join( '(SELECT date, SUM(total_gas_consumption) AS drierMachineGasconsumption, SUM(sand_dried_qty) AS driedSand FROM t_driermachinedetails WHERE date >= ' . $this->db->escape($startDate) . ' AND date <= ' . $this->db->escape($endDate) . ' GROUP BY date ) AS drier_summary', 'drier_summary.date = t_gasstockdetails.date', 'left' ) ->where('t_gasstockdetails.date >=', $startDate) ->where('t_gasstockdetails.date <=', $endDate) ->groupBy('t_gasstockdetails.date') ->get() ->getResultArray(); return $result; } }