如何进行分区间数据统计示例
业务场景:统计消费总金额大于1000元的,800到1000元的,500到800元的,以及500元以下的人数。
SELECT COUNT(CASE WHEN IFNULL(total_money,0) >=1000 THEN a.customer_id END) AS '>1000' ,COUNT(CASE WHEN IFNULL(total_money,0) >=800 AND IFNULL(total_money,0) <1000 THEN a.customer_id END) AS '800~1000' ,COUNT(CASE WHEN IFNULL(total_money,0) >=500 AND IFNULL(total_money,0) <800 THEN a.customer_id END) AS '500~800' ,COUNT(CASE WHEN IFNULL(total_money,0) <500 THEN a.customer_id END) AS '<500' FROM mc_userdb.`customer_login` a LEFT JOIN ( SELECT customer_id,SUM(order_money) AS total_money FROM mc_orderdb.`order_master` GROUP BY customer_id) b ON a.`customer_id`=b.`customer_id`