CREATE TEMPORARY TABLE IF NOT EXISTS temp (Months VARCHAR(50),Sequence INT)
AS (
SELECT
CONCAT(MONTHNAME(m1),' ',YEAR(m1)) AS Months,CONVERT(m1,DATE) AS Sequence
FROM
(
SELECT
('2014-01-01' - INTERVAL DAYOFMONTH('2014-01-01')-1 DAY)
+INTERVAL m MONTH AS m1
FROM
(
SELECT @rownum:=@rownum+1 AS m FROM
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) t1,
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) t2,
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) t3,
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) t4,
(SELECT @rownum:=-1) t0
) d1
) d2
WHERE m1<= '2015-07-30'
ORDER BY m1
) ;
SELECT t.Months,A.OpenCount,A.CloseCount FROM Temp T
LEFT JOIN ( SELECT CONCAT(MONTHNAME(e.dtcdate),' ',YEAR(e.dtcdate)) AS Months,
( SELECT COUNT(csr.ddlcsstatus) FROM csrcrn_frmempengagreqs csr
JOIN master_detail md ON md.masterDetailId=csr.ddlcsstatus WHERE md.abbr='open' AND csr.dtcdate >='2014-01-01' AND csr.dtcdate <='2015-07-30' AND csr.ddlArea=e.ddlArea ) AS OpenCount
,
( SELECT COUNT(csr.ddlcsstatus) FROM csrcrn_frmempengagreqs csr
JOIN master_detail md ON md.masterDetailId=csr.ddlcsstatus WHERE md.abbr='Close' AND csr.dtcdate >='2014-01-01' AND csr.dtcdate <='2015-07-30' AND csr.ddlArea=e.ddlArea ) AS CloseCount
FROM csrcrn_frmempengagreqs e
INNER JOIN master_detail m ON e.ddlcsstatus=m.masterDetailId
WHERE e.dtcdate >='2014-01-01' AND e.dtcdate <='2015-07-30'
GROUP BY MONTH(e.dtcdate) ORDER BY e.dtcdate
) A ON CONVERT(A.Months,CHAR(20))=CONVERT(T.Months,CHAR(20))
ORDER BY T.Sequence;
DROP TEMPORARY TABLE IF EXISTS temp;
/ *给定查询成功给出结果...当将此查询放入USP时,显示错误plz帮助我..proc在下面给出* /
DELIMITER $$
DROP PROCEDURE IF EXISTS `usp_GetEngMonthlyChart_Test`$$
CREATE DEFINER=`root`@`%` PROCEDURE `usp_GetEngMonthlyChart_Test`(IN DateFrom DATE,IN DateTo DATE)
BEGIN
-- SET @strWhere= CONCAT(' AND CSR.dtcInductionDate BETWEEN ''',CONVERT(DateFrom,DATE),''' AND ','''',CONVERT(DateTo,DATE),'''');
SET @strSql=CONCAT('
CREATE TEMPORARY TABLE IF NOT EXISTS temp (Months VARCHAR(50),Sequence INT)
AS (
SELECT
CONCAT(MONTHNAME(m1),'' '',YEAR(m1)) AS Months,CONVERT(m1,DATE) AS Sequence
FROM
(
SELECT
(''',DateFrom,''' - INTERVAL DAYOFMONTH(''',DateFrom,''')-1 DAY)
+INTERVAL m MONTH AS m1
FROM
(
SELECT @rownum:=@rownum+1 AS m FROM
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) t1,
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) t2,
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) t3,
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) t4,
(SELECT @rownum:=-1) t0
) d1
) d2
WHERE m1<= ''',DateTo,'''
ORDER BY m1
)' );
SET @strSql=CONCAT(@strSql,'; GO SELECT t.Months,A.OpenCount,A.CloseCount FROM Temp T
LEFT JOIN ( SELECT CONCAT(MONTHNAME(e.dtcdate),'' '',YEAR(e.dtcdate)) AS Months,
( SELECT COUNT(csr.ddlcsstatus) FROM csrcrn_frmempengagreqs csr
JOIN master_detail md ON md.masterDetailId=csr.ddlcsstatus WHERE md.abbr=''open'' AND csr.dtcdate >=''',DateFrom,
''' AND csr.dtcdate <=''',DateTo,''' AND csr.ddlArea=e.ddlArea ) AS OpenCount
,
( SELECT COUNT(csr.ddlcsstatus) FROM csrcrn_frmempengagreqs csr
JOIN master_detail md ON md.masterDetailId=csr.ddlcsstatus WHERE md.abbr=''Close'' AND csr.dtcdate >=''',DateFrom,
''' AND csr.dtcdate <=''',DateTo,''' AND csr.ddlArea=e.ddlArea ) AS CloseCount
FROM csrcrn_frmempengagreqs e
INNER JOIN master_detail m ON e.ddlcsstatus=m.masterDetailId
WHERE e.dtcdate >=''',DateFrom,''' AND e.dtcdate <=''',DateTo,'''
GROUP BY MONTH(e.dtcdate) ORDER BY e.dtcdate
) A ON CONVERT(A.Months,CHAR(20))=CONVERT(T.Months,CHAR(20))
ORDER BY T.Sequence;
DROP TEMPORARY TABLE IF EXISTS temp;');
SELECT @strSql;
PREPARE stmt FROM @strSql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;
致电usp_GetEngMonthlyChart_Test('2014-01-01','2015-07-30')
DROP
使用临时MEMORY表。我假设正确吗?