Ja, det er muligt at bruge en Dynamisk SQL . Vi kan oprette en forespørgselsstreng og derefter forberede og udføre den. Dette giver os mulighed for at angive dynamisk kolonnenavn, hvilket ikke er muligt direkte.
Du skal også bruge en parameter mere for at angive, hvilken kolonne der skal bruges i 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 ;