{
delimiter &&
CREATE PROCEDURE `BPM_CLMBR_RPT_PROC` (IN customer_id INT,IN fromdate date,IN todate date)
BEGIN
-- distingushing the columns up,didntmove,down as well as group concatination
SELECT
RESPONSEDATE ,
ifnull(GROUP_CONCAT(CLIMBER, ''),0) AS 'up',
ifnull(GROUP_CONCAT(STATIC, ''),0) AS 'didntmove',
ifnull(GROUP_CONCAT(FALLER, ''),0) AS 'down',
ID
FROM
(
-- transposing the rows to column
SELECT
CASE
WHEN report = 'STATIC' THEN count
END AS 'STATIC',
CASE
WHEN report = 'CLIMBER' THEN count
END AS 'CLIMBER',
CASE
WHEN REPORT = 'FALLER' THEN COUNT
END AS 'FALLER',
RESPONSEDATE,
ID
FROM
(
-- counting the number of records in STATIC,CLIMBER,FALLER
SELECT
REPORT,RESPONSEDATE,COUNT(REPORT) count,ID
FROM (
-- grouping the records into STATIC,CLIMBER and FALLER
SELECT
CASE
WHEN (FNL.PREVIOUS_SCORE BETWEEN 0 AND 6 AND FNL.CURRENT_SCORE BETWEEN 0 AND 6) THEN 'STATIC'
WHEN (FNL.PREVIOUS_SCORE BETWEEN 7 AND 8 AND FNL.CURRENT_SCORE BETWEEN 7 AND 8) THEN 'STATIC'
WHEN (FNL.PREVIOUS_SCORE BETWEEN 9 AND 10 AND FNL.CURRENT_SCORE BETWEEN 9 AND 10) THEN 'STATIC'
WHEN (FNL.PREVIOUS_SCORE BETWEEN 0 AND 6 AND FNL.CURRENT_SCORE BETWEEN 7 AND 8) THEN 'CLIMBER'
WHEN (FNL.PREVIOUS_SCORE BETWEEN 0 AND 6 AND FNL.CURRENT_SCORE BETWEEN 9 AND 10) THEN 'CLIMBER'
WHEN (FNL.PREVIOUS_SCORE BETWEEN 7 AND 8 AND FNL.CURRENT_SCORE BETWEEN 9 AND 10) THEN 'CLIMBER'
WHEN (FNL.PREVIOUS_SCORE BETWEEN 7 AND 8 AND FNL.CURRENT_SCORE BETWEEN 0 AND 6) THEN 'FALLER'
WHEN (FNL.PREVIOUS_SCORE BETWEEN 9 AND 10 AND FNL.CURRENT_SCORE BETWEEN 0 AND 6) THEN 'FALLER'
WHEN (FNL.PREVIOUS_SCORE BETWEEN 9 AND 10 AND FNL.CURRENT_SCORE BETWEEN 7 AND 8) THEN 'FALLER'
END AS 'REPORT',
FNL.RESPONSEDATE,
ID
FROM (
SELECT
-- group concatination using space but only one client responses is expected
CLIENTID,
CLIENTNAME,
GROUP_CONCAT(TEMP.CURRENT_SCORE, '') AS 'CURRENT_SCORE',
GROUP_CONCAT(TEMP.PREVIOUS_SCORE, '') AS 'PREVIOUS_SCORE',
GROUP_CONCAT(TEMP.COMMENT,'') AS 'COMMENT',
max(id) ID,
max(responsedate) RESPONSEDATE
FROM
(
-- Top two npsscores are taken into account and only 1st comment is taken
SELECT
CLIENTID,id,CLIENTNAME,
CASE
WHEN RANK = 1 THEN npsscore
END AS 'CURRENT_SCORE',
CASE
WHEN RANK = 2 THEN npsscore
END AS 'PREVIOUS_SCORE',
CASE
WHEN RANK=1 THEN COMMENT
END AS 'COMMENT',
RESPONSEDATE
FROM
(
-- NPS_CLIENT_STATUS is calculated and only the 1st and 2nd rank holders are selected
SELECT
CLMBR_RPT.*,CASE
WHEN CLMBR_RPT.NPSSCORE BETWEEN 0 AND 6 THEN 'DETRACTOR'
WHEN CLMBR_RPT.NPSSCORE BETWEEN 7 AND 8 THEN 'PASSIVE'
ELSE 'PROMOTER'
END 'NPS_CLIENT_STATUS'
FROM
(
-- rank funtion is in corporated rownum() in Oracle
SELECT
a.*,
(CASE a.CLIENTID
WHEN @curType THEN @curRow:=@curRow + 1
ELSE @curRow:=1 AND @curType:=a.CLIENTID
END) AS rank
FROM
(SELECT
NSR.*
FROM
nps_summary_report NSR
INNER JOIN
-- innerjoin is done to get all the client id for a given date span which jas multiple records in the nps_summary_report table
-- joined with nps_summary_report table will eliminate all the records in the nps_summary_report table which is not having
-- customerid , multiple responses and date
-- this is the innermost query and will execute first and eliminate all the unwanted records
(SELECT
CLIENTID
FROM
nps_summary_report
WHERE CUSTOMERID=customer_id -- TO BE PROVIDED
-- AND RESPONSEDATE between STR_TO_DATE(fromdate, '%m/%d/%Y') AND STR_TO_DATE(todate, '%m/%d/%Y') -- TO BE PROVIDED
AND RESPONSEDATE > fromdate and RESPONSEDATE< todate -- TO BE PROVIDED
-- AND RESPONSEDATE BETWEEN date_format(STR_TO_DATE(@fromdate, '%m/%d/%Y'),'%Y-%m-%d') AND date_format(STR_TO_DATE(@todate, '%m/%d/%Y'),'%Y-%m-%d')
-- AND RESPONSEDATE BETWEEN date_format(@fromdate,'%Y-%m-%d') AND date_format(@todate,'%Y-%m-%d')
GROUP BY CLIENTID
HAVING COUNT(*) > 1) CLNT_ID ON CLNT_ID.CLIENTID = NSR.CLIENTID ) a, -- REMOVING ALL THE CLIENT WHICH HAVE 1 FEEDBACK
(SELECT @curRow:=0, @curType:='') r
WHERE CUSTOMERID=customer_id -- RETREIVE ALL DATA FOR BOOKMYSHOW ONLY
ORDER BY CLIENTID , RESPONSEDATE ) CLMBR_RPT
WHERE
CLMBR_RPT.RANK BETWEEN 0 AND 2)STG_BMS_CLIMBER_RPT
GROUP BY CLIENTID , CASE
WHEN RANK = 1 THEN npsscore
END , CASE
WHEN RANK = 2 THEN npsscore
END,CASE
WHEN RANK=1 THEN COMMENT
END
) TEMP
GROUP BY CLIENTID) FNL ) TBL
GROUP BY RESPONSEDATE,REPORT)stg_temp) TEMP
GROUP BY RESPONSEDATE
order by RESPONSEDATE desc;
END
&&
delimiter ; }
我通过
调用存储过程{ call BPM_CLMBR_RPT_PROC(6,str_to_date('2015-10-01','%Y-%m-%d'),str_to_date('2015-12-31','%Y-%m-%d')); }
查询返回所有记录而不考虑日期约束。但是当在代码中传递硬编码值时
{
RESPONSEDATE BETWEEN date_format(STR_TO_DATE('11/1/2015', '%m/%d/%Y'),'%Y-%m-%d') AND date_format(STR_TO_DATE('12/13/2015', '%m/%d/%Y'),'%Y-%m-%d')
}
然后预期结果即将到来。