SELECT city,
sum(quantity) AS quantity,
ifnull(sum(revenue) / sum(quantity), 0) AS averageprice
FROM fact.`order` f
JOIN dim.date d ON f.date = d.date
WHERE d.date BETWEEN ? AND ?
GROUP BY d.month_num_overall
ORDER BY d.month_num_overall DESC,d.month_num_overall ASC
LIMIT 3, 3
SELECT focus,
truncate(1 - 1.0 / count(*) * (2 * sum(base.accumulate_revenue) / max(base.accumulate_revenue) -1), 2) AS g
FROM
(SELECT @cs := CASE
WHEN @focus != raw.focus THEN raw.revenue
ELSE @cs + raw.revenue
END AS accumulate_revenue, @focus := raw. focus AS focus
FROM
(SELECT @cs := 0) cs_idx
JOIN
(SELECT @focus:='') s_idx
JOIN
( SELECT f.FOCUS AS focus,
f.revenue
FROM fact.`order` f
WHERE d.date be tween ?
AND ?
AND coupontype != 4
ORDER BY focus,
f.revenue) raw) base
GROUP BY base.focus