I have created the following function but seem to be getting an error.
DELIMITER $$
CREATE FUNCTION avg_month(shop INTEGER) RETURNS DECIMAL
BEGIN
DECLARE
avg_m_sales DECIMAL
SELECT TRUNCATE((COUNT(paymentdate) / 12),2) FROM sales_table
WHERE YEAR(paymentdate) = 2017 AND shopid = shop
RETURN avg_m_sales
END $$
DELIMITER ;
I am getting the following error.
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT TRUNCATE((COUNT(payid) / 12),2) FROM fss_Payment WHERE YEAR(paydate) ' at line 5
I also tried adding ; to the end of the statements but when I do this i get the following error:
#1415 - Not allowed to return a result set from a function
;at the end there on your statements.#1415 - Not allowed to return a result set from a functionavg_m_salesand returning it, but not actually assigning its value in theSELECTstatement? (I don't know MySQL functions, but my guess would be that without the assignment to a variable, MySQL thinks you want to return the resultset ofSELECTfrom the function, as would happen in a stored procedure.)SET avg_m_sales = (SELECT ... );. Or just skip thatavg_m_salesvariable -RETURN (SELECT ... )might also work.