adkt
3/1/2018 - 3:17 AM

Divide

Relational Algebra

Divide With Remainder. "Source" (the name of a Table) will match all elements that have at least one of EACH element in the "Numerator" (also the name of a Table)'.

DELIMITER //
CREATE PROCEDURE divideWR(IN columnToDivide text, IN columnToDivideWith text, IN tableSource text, IN tableNumerator text)
SQL SECURITY INVOKER
COMMENT 'Divide With Remainder. "Source" (the name of a Table) will match all elements that have at least one of EACH element in the "Numerator" (also the name of a Table)'
BEGIN
 SET @transaction1 = CONCAT(
         'SELECT DISTINCT ',
         columnToDivide,
         ' FROM ',
         tableSource,
         ' AS TAB1 WHERE NOT EXISTS (SELECT * FROM ',
         tableNumerator,
         ' WHERE NOT EXISTS (SELECT * FROM ',
         tableSource,
         ' AS TAB2 WHERE (TAB1.',
         columnToDivide,
         ' = TAB2.',
         columnToDivide,
         ') AND (TAB2.',
         columnToDivideWith,
         ' = ',
         tableNumerator,
         '.'
         ,columnToDivideWith,
         ')));'
     );
 PREPARE statement1 FROM @transaction1;
 EXECUTE statement1;
 DEALLOCATE PREPARE statement1;
END //
DELIMITER ;