with t1 as(SELECT
YEAR(payment_date) AS income_year,
CONCAT(last_name,' ', first_name) as staff,
SUM(amount) AS total_payments
FROM payment
join staff using(staff_id)
GROUP BY
YEAR(payment_date),
staff
WITH ROLLUP
ORDER BY
income_year IS NULL, -- Сначала строки с годом (NULL в конце)
income_year,
staff IS NULL, -- Сначала строки с staff_id (NULL в конце)
staff)
select
IFNULL(income_year, 'Total') AS income_year,
IFNULL(staff,
CASE WHEN income_year IS NOT NULL THEN 'Year total' ELSE ' ' END
) AS staff,
total_payments
from t1