I have created a stored procedure for the purpose of optimization. Below is the actual code.
DELIMITER $$
CREATE PROCEDURE getImpressions (
IN aff_id BIGINT(20),
IN funn_id BIGINT(20),
IN grpBy TEXT,
IN odrBy TEXT
)
BEGIN
SELECT
affiliate_id,
funnel_id,
COUNT(DISTINCT (decimal_ip)) as no_of_records,
HOUR(CONVERT_TZ(FROM_UNIXTIME(created_timestamp), 'UTC', 'US/Eastern')) AS hour,
CEIL(HOUR(CONVERT_TZ(FROM_UNIXTIME(created_timestamp), 'UTC', 'US/Eastern'))/2) AS hoursby2,
DATE(CONVERT_TZ(FROM_UNIXTIME(created_timestamp), 'UTC', 'US/Eastern')) AS date,
WEEK(CONVERT_TZ(FROM_UNIXTIME(created_timestamp), 'UTC', 'US/Eastern')) AS week,
CEIL(WEEK(CONVERT_TZ(FROM_UNIXTIME(created_timestamp), 'UTC', 'US/Eastern'))/2) AS weeksby2,
MONTH(CONVERT_TZ(FROM_UNIXTIME(created_timestamp), 'UTC', 'US/Eastern')) AS month,
YEAR(CONVERT_TZ(FROM_UNIXTIME(created_timestamp), 'UTC', 'US/Eastern')) AS year
FROM gs_aff_analytics
WHERE affiliate_id = aff_id AND funnel_id = funn_id
GROUP BY grpBy
ORDER BY odrBy
;
END$$
DELIMITER ;
#calling the sp
CALL getImpressions(36, 2, 'date', 'date');
Here everything is working fine. But the grpBy
value which I'm passing through parameter is not working. This means the sp just not taking it even if I'm passing it. But as soon as I recreate the sp and explicitly write the group by
clause as GROUP BY date
(hard coding the group by) it start working properly.