数据统计分析中难免遇到如下情况,总结下,学习学习。
select
ifnull(big_state_name, '全大军汇总') as big_state_name,if(big_state_name is null,'全大军汇总',ifnull(province_name,'全大区汇总')) as province_name,
if(big_state_name is null,'全大军汇总',if(province_name is null,'全大区汇总',ifnull(city_name,'全市汇总'))) as city_name,
sum(businesssum)
from t_sale_customer_order_base
where date(activationdate)=date('20161213') and big_district_name in ('李徐文','罗轩','王小伟')
and big_district_name is not null
group by big_state_name,province_name,city_name
with ROLLUP;
上语句实现了按照大区的分组,同时实现了全国汇总,等同union all 或者 union,就是代码上显的简化些。