Tomek Tomek - 2 months ago 33
MySQL Question

Incorrect usage of WITH ROLLUP and DISTINCT

I try to import sql file to MySql database but I run into a problem:

ERROR 1221 (HY000) at line 4598: Incorrect usage of WITH ROLLUP and DISTINCT

DROP PROCEDURE IF EXISTS `sp_employees_rollup`$$
CREATE PROCEDURE `sp_employees_rollup` ()
BEGIN
SELECT Distinct City ,Sum(Salary) Salary_By_City FROM employees
GROUP BY City WITH ROLLUP;

END $$

DELIMITER ;

Answer

You don't need to use DISTINCT City in a GROUP BY query because because each record in the result set will already be distinct. Use this query instead:

SELECT City
       SUM(Salary) Salary_By_City
FROM employees
GROUP BY City WITH ROLLUP
Comments