Skip to content

Instantly share code, notes, and snippets.

@cosminpopescu14
Created March 11, 2020 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosminpopescu14/637e06c6a0eed7473d6a2f8dbf2e2887 to your computer and use it in GitHub Desktop.
Save cosminpopescu14/637e06c6a0eed7473d6a2f8dbf2e2887 to your computer and use it in GitHub Desktop.
USE `coronavirus`;
DROP procedure IF EXISTS `calculate_growth_rate`;
DELIMITER $$
USE `coronavirus`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `calculate_growth_rate`()
BEGIN
DECLARE last_case INT DEFAULT 0;
DECLARE first_case INT DEFAULT 0;
DECLARE most_recent_date date DEFAULT 0;
DECLARE first_day date DEFAULT 0;
DECLARE growth_rate float default 0.00;
set @last_case := (select cases from coronavirus.cases
order by id desc
limit 1);
set @first_case := (select cases from coronavirus.cases
order by id asc
limit 1);
set @most_recent_date := (select date from coronavirus.cases
order by id desc
limit 1);
set @first_day := (select date from coronavirus.cases
order by id asc
limit 1);
set @growth_rate := ((@last_case - @first_case) / @first_case * 100) / (select datediff(@most_recent_date, @first_day));
select @growth_rate as growth_rate;
END$$
DELIMITER ;
@cosminpopescu14
Copy link
Author

A stored procedure for determine growth rate
Formula is from here
https://pages.uoregon.edu/rgp/PPPM613/class8a.htm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment