# 练习2. 从dw_complain_total这个表中列出201509各区域的投诉量。# 注意:group by area,记住在前面select字段的时候还要有一个相同字段areaselectsum(comp_total)as c_t, area
from dw_complain_total
where DATE_FORMAT(datetime,'%Y%m')='201509'groupby area;
# 练习3. 从dw_complain_total这个表中列出2015年09月份北京的 投诉处理率;保留两位小数点,加上百分号# 注意:保留小数点用的是round()函数;百分号想到用字符串拼接concat# 思路递进# 初步:select comp_total as c_t, area
from dw_complain_total
where DATE_FORMAT(datetime,'%Y%m')='201509'and area ='北京';# 优化后:selectsum(comp_processed_total)/sum(comp_total)as c_t, area
from dw_complain_total
where DATE_FORMAT(datetime,'%Y%m')='201509'and area ='北京';# 再优化后:select concat(round(sum(comp_processed_total)/sum(comp_total)*100,2),'%')as c_t, area
from dw_complain_total
where DATE_FORMAT(datetime,'%Y%m')='201509'and area ='北京';
练习4. 2015年9月份北京的投诉类型及投诉数量
# 练习4. 2015年9月份北京的投诉类型及投诉数量# 注意:寻找另一个表,双表联立,找到外键,group by什么想清楚肯定是跟着挑选出来的字段的,如何按照降序排列,SELECT
b.zw_name,sum(a.complain_total)as comptotal
from dw_complain_tslx a JOIN dm_tslx b on a.tslx_code = b.zd_name
where DATE_FORMAT(datetime,'%Y%m')='201509'and area ='北京'groupby b.zw_name orderby comptotal desclimit9
练习5 列出2015年9月份投诉来源省top 10,还可以限定是投诉北京的(不是来源)
# 练习5 列出2015年9月份投诉来源省top 10,还可以限定是投诉北京的(不是来源)# 注意:还是得选择专业的表;,group by什么想清楚肯定是跟着挑选出来的字段的-- select -- sum(comp_total) as c_t, area-- from dw_complain_total-- where DATE_FORMAT(datetime, '%Y%m') = 201509-- group by area order by comp_total desc limit 10;SELECT
area_ly as province,sum(complain_total)as compltotal
from dw_complain_tsly
where DATE_FORMAT(datetime,'%Y%m')=201509GROUPBY area_ly
ORDERBYsum(complain_total)desclimit10