未来预期销售查询的功能将无法运行-MySQL

时间:2018-11-29 17:38:36

标签: mysql sql function

我有这个查询,可以查询任何给定未来月份的预期销售额。

    DELIMITER //

CREATE FUNCTION get_expected_sales(month INT)
RETURNS DOUBLE(4, 2)

BEGIN
  DECLARE month INT;
  DECLARE total_sales_2015 INT;
  DECLARE total_sales_2016 INT;
  DECLARE total_sales_2017 INT;
  DECLARE expected_sales DOUBLE(5, 2);

  SELECT SUM(amount) INTO total_sales_2015
  FROM fss_Payment payTableRef
  WHERE MONTH(paydate)=month AND YEAR(paydate)=2015;

  SELECT SUM(amount) INTO total_sales_2016
  FROM fss_Payment payTableRef
  WHERE MONTH(paydate)=month AND YEAR(paydate)=2016;

  SELECT SUM(amount) INTO total_sales_2017
  FROM fss_Payment payTableRef
  WHERE MONTH(paydate)=month AND YEAR(paydate)=2017;

  SET expected_sales = ROUND((total_sales_2015+total_sales_2016+total_sales_2017)/3,2);

  RETURN expected_sales;

  END //

  DELIMITER;

我得到的主要错误是:“函数已经存在”和“第15行的WHERE子句中的错误”

0 个答案:

没有答案