Ya, dimungkinkan menggunakan SQL Dinamis . Kita dapat membuat string kueri, lalu menyiapkan dan menjalankannya. Ini akan memungkinkan kita untuk menentukan nama kolom dinamis, yang tidak mungkin secara langsung.
Anda juga perlu menggunakan satu parameter lagi untuk menentukan kolom mana yang akan digunakan di 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 ;