I have a table where I have fields as open_date and a close_date. I have to create a procedure for which, given a range of dates as input, I show all the records for each date that lies within the range, the records whose open_date>date and closed_date
for example if my table is
id open_date closed_date
1 2016-06-06 2016-06-10
2 2016-06-08 2016-06-11
Date Count
2016-06-07 1
2016-06-08 2
2016-06-09 2
Date Count
2016-06-09 2
CREATE DEFINER=`root`@`localhost` PROCEDURE `find_number_of_bugs`(IN startDate DateTIme, IN endDate DateTime)
BEGIN
Declare testDate Date;
SET testDate=startDate;
while testDate<=endDate DO
select testDate as Dates ,count(bugID) as Number_Of_Bugs_Open from bugs where testDate between open_date and close_date OR close_date=null;
SET testDate=DATE_ADD(testDate,INTERVAL 1 DAY);
END while;
END
You are gutting all the results but in multiple result sets. You can save all query results in a temporary table and query it once at the end.
CREATE DEFINER=`root`@`localhost` PROCEDURE `find_number_of_bugs`(IN startDate DateTIme, IN endDate DateTime)
BEGIN
Declare testDate Date;
SET testDate=startDate;
CREATE TEMPORARY TABLE output (Dates date,Number_Of_Bugs_Open int);
while testDate<=endDate DO
insert into output select testDate as Dates ,count(bugID) as Number_Of_Bugs_Open from bugs where testDate between open_date and close_date OR close_date=null;
SET testDate=DATE_ADD(testDate,INTERVAL 1 DAY);
END while;
select * from output;
END