/* COUNT REMINDERS LINKED TO MEDICATION THAT'S SKIPPED */
/* =============================================================================================================== */
DROP FUNCTION IF EXISTS COUNT_SKIPPED_REMINDERS;
COMMIT;

delimiter //

CREATE FUNCTION `COUNT_SKIPPED_REMINDERS`(`p_userId` varchar(36), `p_start` datetime, `p_end` datetime)
RETURNS INT
BEGIN

	DECLARE noReminders int;
	
	SET noReminders = (
    SELECT COUNT(*)
    FROM `Reminders`
    WHERE `userId` = p_userId
    AND `timestamp` >= p_start
    AND `timestamp` < p_end
    AND `status` IN ('skipped', 'missed')
    AND `dependant` IS NULL
  );
	
	RETURN noReminders;

END 
//
COMMIT //

delimiter ;
