SET @PERIOD_MIN = 154;
SET @PERIOD_MAX = 158;



SELECT
	A1.`期数` AS 期数,
	COUNT(ELT(INTERVAL(A1.`总奖金`,0,99.99),'<100')) AS '<100',
	COUNT(ELT(INTERVAL(A1.`总奖金`,100,999.99),'100-1000')) AS '100-1000',
	COUNT(ELT(INTERVAL(A1.`总奖金`,1000,1999.99),'1000-2000')) AS '1000-2000',
	COUNT(ELT(INTERVAL(A1.`总奖金`,2000,2999.99),'2000-3000')) AS '2000-3000',
	COUNT(ELT(INTERVAL(A1.`总奖金`,3000,3999.99),'3000-4000')) AS '3000-4000',
	COUNT(ELT(INTERVAL(A1.`总奖金`,4000,4999.99),'4000-5000')) AS '4000-5000',
	COUNT(ELT(INTERVAL(A1.`总奖金`,5000),'>5000')) AS '>5000',
	COUNT(A1.USER_ID) AS 总计
FROM
	(SELECT
	B.PERIOD_NUM AS 期数,
	B.USER_ID,
	B.BONUS_TOTAL AS 总奖金
FROM
	ar_calc_bonus B
	WHERE B.PERIOD_NUM >= @PERIOD_MIN AND B.PERIOD_NUM <= @PERIOD_MAX AND B.BONUS_TOTAL >= 1
	UNION ALL
SELECT
	J.`期数`,
	J.UID,
	J.`总奖金`
FROM
	`奖金数据表` J
	WHERE J.`期数` >= @PERIOD_MIN AND J.`期数` <= @PERIOD_MAX AND J.`总奖金` >= 1)A1
	GROUP BY A1.`期数`
