Sí, es posible usar un SQL dinámico . Podemos crear una cadena de consulta y luego prepararla y ejecutarla. Esto nos permitirá especificar el nombre de la columna dinámica, lo que no es posible directamente.
También deberá usar un parámetro más para especificar qué columna usar en Group By
DELIMITER $$
CREATE DEFINER=`root`@`localhost`
PROCEDURE `GetChartApprovedData`(in siteValue varchar(45),
in skillValue varchar(100),
in shiftValue varchar(100),
in tmValue varchar(45),
in grpmValue varchar(45),
in dateValue date,
in dateValue1 date,
in groupByColumn varchar(64))
-- add extra in parameter, groupByColumn, to specify which column to group upon
BEGIN
SET query_str = CONCAT('SELECT
count(agentlogin) AS totalApproved,
shift AS Shift,
skill AS Skill,
tm AS TM,
grpM AS GrpM
FROM approved
WHERE (sitevalue IS NULL
OR site = sitevalue)
AND (skillvalue IS NULL
OR skill = skillvalue)
AND (shiftvalue IS NULL
OR shift = shiftvalue)
AND (tmValue IS NULL
OR tm = tmValue)
AND (grpmValue IS NULL
OR grpM = grpmValue)
AND (dateValue IS NULL
OR date BETWEEN dateValue AND dateValue1)
GROUP BY ',
groupByColumn); -- concatenate the group by column param
-- prepare the query
PREPARE stmt FROM query_str;
-- execute the query
EXECUTE stmt;
-- Clear up
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;