set the whole sql query in a SET @sql='' variable with appropriate changes in sql quotes and all 
WITH filtered_result AS (
    SELECT
    DISTINCT
        t_asm_detail.asm_id,
        t_asm_detail.wafer_no AS `wafer_no`,
        t_pcsjb_info.carrier_id AS `pcsjbInfo.carrier_id`,
        t_asm_detail.chamber_id AS chamber_id,
        t_chamber_recipe_info.recipe_no AS `chamberRecipeInfo.recipe_no`,		
		t_chamber_recipe_info.recovery_flg AS `chamberRecipeInfo.recovery_flg`,
        t_asm_detail.process_recipe_name AS `process_recipe_name`,
        t_asm_detail.start_date AS `start_date`,
        t_asm_detail.end_date  AS `end_date`
    FROM
        t_asm_detail
    LEFT JOIN t_chamber_recipe_info ON t_asm_detail.recipe_no = t_chamber_recipe_info.recipe_no
    LEFT JOIN t_chamber_unique_info As tcui ON t_asm_detail.chamber_id = tcui.chamber_unique_id
    LEFT JOIN t_pcsjb_info ON tcui.device_unique_id = t_pcsjb_info.device_unique_id
        AND SUBSTRING(process_recipe_name, LOCATE(' ', process_recipe_name) + 1) = t_pcsjb_info.seq_rcp_name
        AND start_date >= t_pcsjb_info.pj_start_time
        AND t_pcsjb_info.pj_end_time >= end_date
    WHERE
        t_chamber_recipe_info.recipe_no != 0
        AND t_chamber_recipe_info.recovery_flg = 0
        AND FIND_IN_SET(TRIM(REGEXP_REPLACE(chamber_id, '[[:space:]]+', ' ')) ,chamber_id_param)
        AND start_date >= start_date_param
        AND end_date <= end_date_param
),
sorted_result AS (
    SELECT
        *,
        IF(@prev_key != asm_id, @row_index := @row_index + 1, @row_index) AS row_index,
        @prev_key := asm_id
    FROM
        filtered_result
    ORDER BY
         case when sortClause_param='`wafer_no` ' then `wafer_no` else `wafer_no`  end ,
        asm_id
)
SELECT
    TRIM(REGEXP_REPLACE(sr.asm_id, '[[:space:]]+', ' ')) AS `asm_id`,
    TRIM(REGEXP_REPLACE(sr.wafer_no, '[[:space:]]+', ' ')) AS `wafer_no`,
    TRIM(REGEXP_REPLACE(sr.start_date, '[[:space:]]+', ' ')) AS `start_date`,
    TRIM(REGEXP_REPLACE(sr.end_date, '[[:space:]]+', ' ')) AS `end_date`,     					
	TRIM(REGEXP_REPLACE(sr.`pcsjbInfo.carrier_id`, '[[:space:]]+', ' ')) AS `pcsjbInfo.carrier_id`,               					
	TRIM(REGEXP_REPLACE(cui.device_name, '[[:space:]]+', ' ')) AS `chamberUniqueInfo.device_name`,     					
	TRIM(REGEXP_REPLACE(dui.device_name, '[[:space:]]+', ' ')) AS `chamberUniqueInfo.deviceUniqueInfoDomain.device_name`,
    TRIM(REGEXP_REPLACE(sr.`chamberRecipeInfo.recipe_no`, '[[:space:]]+', ' ')) AS `chamberRecipeInfo.recipe_no`,  					
	TRIM(REGEXP_REPLACE(sr.process_recipe_name, '[[:space:]]+', ' ')) AS `process_recipe_name`,  					
	TRIM(REGEXP_REPLACE(m.measure_hash, '[[:space:]]+', ' ')) AS `trnMapping.measure_hash`,
    (SELECT COUNT(asm_id) FROM filtered_result) as total
    
FROM
    sorted_result sr
LEFT JOIN t_chamber_unique_info cui ON sr.chamber_id = cui.chamber_unique_id
LEFT JOIN t_device_unique_info dui ON cui.device_unique_id = dui.device_unique_id
LEFT JOIN t_mapping m ON sr.asm_id = m.asm_id AND m.del_flg = '1'
-- WHERE  row_index >=row_offset  AND row_index < row_offset + row_count; -- (uncomment for pagination)
 WHERE row_index >= row_offset
AND row_index < (CASE WHEN row_count = 0 THEN (SELECT COUNT(asm_id) FROM filtered_result) ELSE row_offset + row_count END);